summaryrefslogtreecommitdiff
path: root/src/common/fqterm_path.cpp
blob: 7921da1261775f78fcdce429c50bfed57d7b3caf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/***************************************************************************
 *   fqterm, a terminal emulator for both BBS and *nix.                    *
 *   Copyright (C) 2008 fqterm development group.                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.               *
 ***************************************************************************/

#ifdef WIN32
#include <windows.h>
#include <shlobj.h>
#endif // WIN32

#include <QApplication>
#include <QFile>
#include <QDir>
#include <QFont>

#include "fqterm_config.h"
#include "fqterm_trace.h"
#include "fqterm_param.h"
#include "fqterm_path.h"
#include "fqterm_font.h"
#include "../protocol/fqterm_local_socket.h"

namespace FQTerm {
QString* local_shell_bin = NULL;
static QString getUserDataDir();
static QString getInstallPrefix();
static QString getResourceDir(const QString &prefix);

const QString &getPath(PathCategory category) {
  static QString null_dir = "";
  static QString user_config = getUserDataDir();
  static QString prefix = getInstallPrefix();
  static QString resource = getResourceDir(prefix);
  
  switch (category) {
    case RESOURCE:
      return resource;
      break;
    case USER_CONFIG:
      return user_config;
      break;
  }
  return null_dir;
}

void clearDir(const QString &path) {
  QDir dir(path);
  if (dir.exists()) {
    const QFileInfoList list = dir.entryInfoList();
    //QFileInfoListIterator it( *list );
    //QFileInfo *fi;
    foreach(QFileInfo fi, list) {
      if (fi.isFile()) {
        dir.remove(fi.fileName());
      }
    }
  }
}

bool checkPath(const QString &path) {
  QDir dir(path);
  if (!dir.exists()) {
    if (!dir.mkpath(path)) {
      FQ_TRACE("path", 0) << "Failed to create directory " << path;
      return false;
    }
  }
  return true;
}

bool checkFile(const QString &src, const QString &dst) {
  if (QFile(dst).exists()) {
    return true;
  }

  if (!QFile::copy(src, dst)) {
    FQ_TRACE("path", 0) << "Failed to copy a file from " << src
                        << " to " << dst;
    return false;
  } 

  FQ_TRACE("path", 5) << "A file copied from " << src
                      << " to " << dst;

  if (!QFile::setPermissions(dst, QFile::ReadOwner | QFile::WriteOwner)) {
    FQ_TRACE("path", 0) << "Failed to change access attributs of "<< dst
                        << " to make only the owner have rights to "
                        << "read/write it.";
    return false;
  }

  return true;
}

bool iniSettings() {
  if (!checkPath(getPath(USER_CONFIG))) {
    return false;
  }

  if (!checkFile(getPath(RESOURCE) + "userconf/fqterm.cfg.orig", 
                 getPath(USER_CONFIG) + "fqterm.cfg")) {
    return false;
  }

  if (!checkFile(getPath(RESOURCE) + "userconf/address.cfg.orig", 
                 getPath(USER_CONFIG) + "address.cfg")) {
    return false;
  }

  if (!checkFile(getPath(RESOURCE) + "userconf/language.cfg.orig", 
                 getPath(USER_CONFIG) + "language.cfg")) {
    return false;
  }

  //Copy schema files
  if (checkPath(getPath(USER_CONFIG) + "schema")) {
    checkFile(getPath(RESOURCE) + "schema/default.schema", 
      getPath(USER_CONFIG) + "schema/default.schema");

    checkFile(getPath(RESOURCE) + "schema/Linux.schema", 
      getPath(USER_CONFIG) + "schema/Linux.schema");

    checkFile(getPath(RESOURCE) + "schema/Softness.schema", 
      getPath(USER_CONFIG) + "schema/Softness.schema");

    checkFile(getPath(RESOURCE) + "schema/VIM.schema", 
      getPath(USER_CONFIG) + "schema/VIM.schema");

    checkFile(getPath(RESOURCE) + "schema/XTerm.schema", 
      getPath(USER_CONFIG) + "schema/XTerm.schema");
  }


  //read settings from fqterm.cfg
  FQTermConfig *conf = new FQTermConfig(getPath(USER_CONFIG) + "fqterm.cfg");

  //set font
  QString family = (conf->getItemValue("global", "font"));

  QString pointsize = conf->getItemValue("global", "pointsize");
  QString pixelsize = conf->getItemValue("global", "pixelsize");
  if (!family.isEmpty()) {
    QFont font(family);
    if (pointsize.toInt() > 0) {
      font.setPointSize(pointsize.toInt());
    }
    if (pixelsize.toInt() > 0) {
      font.setPixelSize(pixelsize.toInt());
    }
    QString openAntiAlias_ = conf->getItemValue("global", "antialias");
    if (openAntiAlias_ != "0") {
      font.setStyleStrategy(QFont::PreferAntialias);
    }
    qApp->setFont(font);
  }

  // zmodem and pool directory
  QString pathZmodem = conf->getItemValue("preference", "zmodem");
  if (pathZmodem.isEmpty()) {
    pathZmodem = getPath(USER_CONFIG) + "zmodem";
  }

  if (!checkPath(pathZmodem)) {
    return false;
  }

  QString pathPool = conf->getItemValue("preference", "pool");

  if (pathPool.isEmpty()) {
    pathPool = getPath(USER_CONFIG) + "pool/";
  }

  if (pathPool.right(1) != "/") {
    pathPool.append('/');
  }

  QString pathCache = pathPool + "shadow-cache/";

  if (!checkPath(pathPool) || !checkPath(pathCache)) {
    return false;
  }

  // fqterm local socket cmdline
  QString externSSH = conf->getItemValue("global", "externSSH");
  if (!externSSH.isEmpty())
    local_shell_bin = new QString(externSSH);

  delete conf;
  return true;
}

void checkHelpExists(FQTermConfig* pConf) {
  QString strTmp = pConf->getItemValue("bbs list", "num");
  int bbsCount = strTmp.toInt();

  QString strSection;

  for (int i = 0; i < bbsCount; i++) {
    strSection.sprintf("bbs %d", i);
    strTmp = pConf->getItemValue(strSection, "name");
    if (strTmp == "FQTermHelp")
      return;
  }
  pConf->setItemValue("bbs list", "num", QString("%1").arg(bbsCount + 1));
  saveAddress(pConf, bbsCount, FQTermParam::getFQBBSParam());
  pConf->save(getPath(USER_CONFIG) + "address.cfg");
}

void loadNameList(FQTermConfig *pConf, QStringList &listName) {
  QString strTmp = pConf->getItemValue("bbs list", "num");

  QString strSection;

  for (int i = 0; i < strTmp.toInt(); i++) {
    strSection.sprintf("bbs %d", i);
    listName.append(pConf->getItemValue(strSection, "name"));
  }
}

bool loadAddress(FQTermConfig *pConf, int n, FQTermParam &param) {
  QString strTmp, strSection;
  if (n < 0) {
    strSection = "default";
  } else {
    strSection.sprintf("bbs %d", n);
  }

  if (!pConf->hasSection(strSection))
	  return false;

  // check if larger than existence
  strTmp = pConf->getItemValue("bbs list", "num");
  if (n >= strTmp.toInt()) {
    return false;
  }
  param.name_ = pConf->getItemValue(strSection, "name");
  param.hostAddress_ = pConf->getItemValue(strSection, "addr");
  strTmp = pConf->getItemValue(strSection, "port");
  param.port_ = strTmp.toUShort();
  strTmp = pConf->getItemValue(strSection, "hosttype");
  param.hostType_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "autologin");
  param.isAutoLogin_ = (strTmp != "0");
  param.preLoginCommand_ = pConf->getItemValue(strSection, "prelogin");
  param.userName_ = pConf->getItemValue(strSection, "user");
  param.password_ = pConf->getItemValue(strSection, "password");
  param.postLoginCommand_ = pConf->getItemValue(strSection, "postlogin");

  strTmp = pConf->getItemValue(strSection, "bbscode");
  param.serverEncodingID_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "autofont");
  if (strTmp == "0") {
    param.isFontAutoFit_ = 0;
  } else if (strTmp == "2") {
    param.isFontAutoFit_ = 2;
  } else {
    param.isFontAutoFit_ = 1;
  }
  strTmp = pConf->getItemValue(strSection, "alwayshighlight");
  param.isAlwaysHighlight_ = (strTmp != "0");
  strTmp = pConf->getItemValue(strSection, "ansicolor");
  param.isAnsiColor_ = (strTmp != "0");
  QString language;
  QString font_name;
  QString font_size;
  language = FQTermParam::getLanguageName(true, false); 
  font_name = pConf->getItemValue(strSection, language + "fontname");
  font_size = pConf->getItemValue(strSection, language + "fontsize");
  if (!font_name.isEmpty()) {
    param.englishFontName_ = font_name;
  }
  if (!font_size.isEmpty()) {
    param.englishFontSize_ = font_size.toInt();
  }
  //FIXME: Should be removed in next release!!!!! (Since 0.9.7)
  if (font_name.isEmpty()) {
    language = FQTermParam::getLanguageName(true); 
    font_name = pConf->getItemValue(strSection, language + "fontname");
    if (!font_name.isEmpty()) {
      param.englishFontName_ = font_name;
    }
  }

  language = FQTermParam::getLanguageName(false, false); 
  font_name = pConf->getItemValue(strSection, language + "fontname");
  font_size = pConf->getItemValue(strSection, language + "fontsize");
  if (!font_name.isEmpty()) {
    param.otherFontName_ = font_name;
  }
  if (!font_size.isEmpty()) {
    param.otherFontSize_ = font_size.toInt();
  }

  //FIXME: Should be removed in next release!!!!!
  if (font_name.isEmpty()) {
    language = FQTermParam::getLanguageName(false); 
    font_name = pConf->getItemValue(strSection, language + "fontname");
    if (!font_name.isEmpty()) {
      param.englishFontName_ = font_name;
    }
  }

  param.foregroundColor_.setNamedColor(pConf->getItemValue(strSection, "fgcolor"));
  param.backgroundColor_.setNamedColor(pConf->getItemValue(strSection, "bgcolor"));
  param.schemaFileName_ = pConf->getItemValue(strSection, "schemafile");

  param.virtualTermType_ = pConf->getItemValue(strSection, "termtype");
  strTmp = pConf->getItemValue(strSection, "keytype");
  param.keyboardType_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "backspacetype");
  param.backspaceType_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "column");
  param.numColumns_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "row");
  param.numRows_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "scroll");
  param.numScrollLines_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "cursor");
  param.cursorType_ = strTmp.toInt();
  param.escapeString_ = pConf->getItemValue(strSection, "escape");

  strTmp = pConf->getItemValue(strSection, "proxytype");
  param.proxyType_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "proxyauth");
  param.isAuthentation_ = (strTmp != "0");
  param.proxyHostName_ = pConf->getItemValue(strSection, "proxyaddr");
  strTmp = pConf->getItemValue(strSection, "proxyport");
  param.proxyPort_ = strTmp.toInt();
  param.proxyUserName_ = pConf->getItemValue(strSection, "proxyuser");
  param.proxyPassword_ = pConf->getItemValue(strSection, "proxypassword");
  strTmp = pConf->getItemValue(strSection, "protocol");
  param.protocolType_ = strTmp.toInt();
  param.sshUserName_ = pConf->getItemValue(strSection, "sshuser");
  param.sshPassword_ = pConf->getItemValue(strSection, "sshpassword");

  strTmp = pConf->getItemValue(strSection, "maxidle");
  param.maxIdleSeconds_ = strTmp.toInt();
  param.replyKeyCombination_ = pConf->getItemValue(strSection, "replykey");
  if (param.replyKeyCombination_.isNull()) {
    Q_ASSERT(false);
  }

  param.antiIdleMessage_ = pConf->getItemValue(strSection, "antiidlestring");
  strTmp = pConf->getItemValue(strSection, "isantiidle");
  param.isAntiIdle_ = (strTmp != "0");
  param.autoReplyMessage_ = pConf->getItemValue(strSection, "autoreply");
  strTmp = pConf->getItemValue(strSection, "bautoreply");
  param.isAutoReply_ = (strTmp != "0");

  strTmp = pConf->getItemValue(strSection, "reconnect");
  param.isAutoReconnect_ = (strTmp != "0");
  strTmp = pConf->getItemValue(strSection, "interval");
  param.reconnectInterval_ = strTmp.toInt();
  strTmp = pConf->getItemValue(strSection, "retrytimes");
  param.retryTimes_ = strTmp.toInt();

  strTmp = pConf->getItemValue(strSection, "autoclosewin");
  param.isAutoCloseWin_ = (strTmp == "1");

  strTmp = pConf->getItemValue(strSection, "alignmode");
  param.alignMode_ = strTmp.toInt();


  strTmp = pConf->getItemValue(strSection, "charspacing");
  param.charSpacing_ = strTmp.toInt();

  strTmp = pConf->getItemValue(strSection, "linespacing");
  param.lineSpacing_ = strTmp.toInt();

  strTmp = pConf->getItemValue(strSection, "loadscript");
  param.isAutoLoadScript_ = (strTmp != "0");
  param.autoLoadedScriptFileName_ = pConf->getItemValue(strSection, "scriptfile");

  strTmp = pConf->getItemValue(strSection, "enablezmodem");
  param.enableZmodem_ = (strTmp != "0");

  strTmp = pConf->getItemValue(strSection, "isbeep");
  param.isBeep_ = (strTmp != "0");

  strTmp = pConf->getItemValue(strSection, "isbuzz");
  param.isBuzz_ = (strTmp != "0");

  strTmp = pConf->getItemValue(strSection, "ismouse");
  param.isSupportMouse_ = (strTmp != "0");

  strTmp = pConf->getItemValue(strSection, "menutype");
  param.menuType_ = strTmp.toInt();
  param.menuColor_.setNamedColor(pConf->getItemValue(strSection, "menucolor"));

  param.isColorCopy_ = (pConf->getItemValue(strSection, "colorcopy") != "0");
  param.isAutoCopy_ = (pConf->getItemValue(strSection, "autocopy") != "0");
  param.isRectSelect_ = (pConf->getItemValue(strSection, "rectselect") != "0");
  param.isAutoWrap_ = (pConf->getItemValue(strSection, "autowrap" ) == "1");

  return true;
}

void saveAddress(FQTermConfig *pConf, int n, const FQTermParam &param) {
  QString strTmp, strSection;
  if (n < 0) {
    strSection = "default";
  } else {
    strSection.sprintf("bbs %d", n);
  }

  pConf->setItemValue(strSection, "name", param.name_);
  pConf->setItemValue(strSection, "addr", param.hostAddress_);
  strTmp.setNum(param.port_);
  pConf->setItemValue(strSection, "port", strTmp);
  strTmp.setNum(param.hostType_);
  pConf->setItemValue(strSection, "hosttype", strTmp);
  pConf->setItemValue(strSection, "autologin", param.isAutoLogin_ ? "1" : "0");
  pConf->setItemValue(strSection, "prelogin", param.preLoginCommand_);
  pConf->setItemValue(strSection, "user", param.userName_);
  pConf->setItemValue(strSection, "password", param.password_);
  pConf->setItemValue(strSection, "postlogin", param.postLoginCommand_);

  strTmp.setNum(param.serverEncodingID_);
  pConf->setItemValue(strSection, "bbscode", strTmp);
  pConf->setItemValue(strSection, "autofont", strTmp.setNum(param.isFontAutoFit_));
  pConf->setItemValue(strSection, "alwayshighlight", param.isAlwaysHighlight_ ?
                      "1" : "0");
  pConf->setItemValue(strSection, "ansicolor", param.isAnsiColor_ ? "1" : "0");

  pConf->setItemValue(strSection, FQTermParam::getLanguageName(true, false) + "fontname", param.englishFontName_);
  strTmp.setNum(param.englishFontSize_);
  pConf->setItemValue(strSection, FQTermParam::getLanguageName(true, false) + "fontsize", strTmp);

  pConf->setItemValue(strSection, FQTermParam::getLanguageName(false, false) + "fontname", param.otherFontName_);
  strTmp.setNum(param.otherFontSize_);
  pConf->setItemValue(strSection, FQTermParam::getLanguageName(false, false) + "fontsize", strTmp);

  pConf->setItemValue(strSection, "alignmode", strTmp.setNum(param.alignMode_));
  pConf->setItemValue(strSection, "charspacing", strTmp.setNum(param.charSpacing_));
  pConf->setItemValue(strSection, "linespacing", strTmp.setNum(param.lineSpacing_));

  pConf->setItemValue(strSection, "bgcolor", param.backgroundColor_.name());
  pConf->setItemValue(strSection, "fgcolor", param.foregroundColor_.name());

  pConf->setItemValue(strSection, "schemafile", param.schemaFileName_);

  pConf->setItemValue(strSection, "termtype", param.virtualTermType_);
  strTmp.setNum(param.keyboardType_);
  pConf->setItemValue(strSection, "keytype", strTmp);
  strTmp.setNum(param.backspaceType_);
  pConf->setItemValue(strSection, "backspacetype", strTmp);
  strTmp.setNum(param.numColumns_);
  pConf->setItemValue(strSection, "column", strTmp);
  strTmp.setNum(param.numRows_);
  pConf->setItemValue(strSection, "row", strTmp);
  strTmp.setNum(param.numScrollLines_);
  pConf->setItemValue(strSection, "scroll", strTmp);
  strTmp.setNum(param.cursorType_);
  pConf->setItemValue(strSection, "cursor", strTmp);
  pConf->setItemValue(strSection, "escape", param.escapeString_);

  strTmp.setNum(param.proxyType_);
  pConf->setItemValue(strSection, "proxytype", strTmp);
  pConf->setItemValue(strSection, "proxyauth", param.isAuthentation_ ? "1" : "0");
  pConf->setItemValue(strSection, "proxyaddr", param.proxyHostName_);
  strTmp.setNum(param.proxyPort_);
  pConf->setItemValue(strSection, "proxyport", strTmp);
  pConf->setItemValue(strSection, "proxyuser", param.proxyUserName_);
  pConf->setItemValue(strSection, "proxypassword", param.proxyPassword_);
  strTmp.setNum(param.protocolType_);
  pConf->setItemValue(strSection, "protocol", strTmp);
  pConf->setItemValue(strSection, "sshuser", param.sshUserName_);
  pConf->setItemValue(strSection, "sshpassword", param.sshPassword_);

  strTmp.setNum(param.maxIdleSeconds_);
  pConf->setItemValue(strSection, "maxidle", strTmp);
  pConf->setItemValue(strSection, "replykey", param.replyKeyCombination_);
  pConf->setItemValue(strSection, "antiidlestring", param.antiIdleMessage_);
  pConf->setItemValue(strSection, "isantiidle", param.isAntiIdle_?"1" : "0");
  pConf->setItemValue(strSection, "bautoreply", param.isAutoReply_ ? "1" : "0");
  pConf->setItemValue(strSection, "autoreply", param.autoReplyMessage_);
  pConf->setItemValue(strSection, "reconnect", param.isAutoReconnect_? "1" : "0");
  strTmp.setNum(param.reconnectInterval_);
  pConf->setItemValue(strSection, "interval", strTmp);
  strTmp.setNum(param.retryTimes_);
  pConf->setItemValue(strSection, "retrytimes", strTmp);
  pConf->setItemValue(strSection, "autoclosewin", param.isAutoCloseWin_? "1" : "0");

  pConf->setItemValue(strSection, "loadscript", param.isAutoLoadScript_ ? "1" : "0");
  pConf->setItemValue(strSection, "scriptfile", param.autoLoadedScriptFileName_);

  pConf->setItemValue(strSection, "enablezmodem", param.enableZmodem_? "1" : "0");

  pConf->setItemValue(strSection, "isbeep", param.isBeep_? "1" : "0");
  pConf->setItemValue(strSection, "isbuzz", param.isBuzz_? "1" : "0");
  pConf->setItemValue(strSection, "ismouse", param.isSupportMouse_? "1" : "0");

  strTmp.setNum(param.menuType_);
  pConf->setItemValue(strSection, "menutype", strTmp);
  pConf->setItemValue(strSection, "menucolor", param.menuColor_.name());

  pConf->setItemValue(strSection, "colorcopy", param.isColorCopy_? "1" : "0");
  pConf->setItemValue(strSection, "autocopy", param.isAutoCopy_? "1" : "0");
  pConf->setItemValue(strSection, "rectselect", param.isRectSelect_? "1" : "0");
  pConf->setItemValue(strSection, "autowrap", param.isAutoWrap_?"1":"0");

}

static QString getResourceDir(const QString &prefix) {
  QString res;

  std::string fqterm_resource;

  char *p = NULL;
  if ((p = getenv("FQTERM_RESOURCE")) != NULL)
      fqterm_resource = p;

  if (fqterm_resource.size() == 0) {
#if defined(WIN32)
    res = prefix;
#elif defined(__APPLE__)
    res = prefix + "../Resources/";
#else
    res = prefix + "share/FQTerm/";  
#endif
  } else {
    res = QString::fromLocal8Bit(fqterm_resource.c_str());    
  }

  if (res[res.size() - 1] != '/')
      res += '/';
  
  return res;
}

static QString getInstallPrefix() {
  QString res;
  
  std::string fqterm_prefix;

  char *p = NULL;
  if ((p = getenv("FQTERM_PREFIX")) != NULL)
      fqterm_prefix = p;

  if (fqterm_prefix.size() == 0) {
    res = QCoreApplication::applicationDirPath() + "/";
  } else {
    res = QString::fromLocal8Bit(fqterm_prefix.c_str());
  }
  
  if (res[res.size() - 1] != '/')
      res += '/';

  return res;
}

static QString getUserDataDir() {
  char *evnDataDir = NULL;
  if ((evnDataDir = getenv("FQTERM_DATADIR")) != NULL) {
    std::string fqterm_data = evnDataDir;
    if (fqterm_data[fqterm_data.size() - 1] != '/' 
#ifdef WIN32
      || fqterm_data[fqterm_data.size() - 1] != '\\'
#endif
      )
      fqterm_data += '/';
    if (fqterm_data.length() != 0) {
      return QString::fromLocal8Bit(fqterm_data.c_str());
    }
  }

#ifdef WIN32
  char buffer[MAX_PATH];
  SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, buffer);
  std::string dir(buffer);

  if (dir[dir.size() - 1] != '\\') {
    dir += '\\';
  }
  return QString::fromLocal8Bit(dir.c_str()) + "FQTerm/";
#else
  char *p = NULL;

  std::string home;
  if ((p = getenv("HOME")) != NULL)
      home = p;
  if (home.size() == 0)
      home = "/root/";    

  if (home[home.size() - 1] != '/')
      home += '/';

  return QString::fromLocal8Bit(home.c_str()) + ".fqterm/";
#endif
}

}  // namespace FQTerm