1 // Copyright (c) 2020-2021 hors<horsicq@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 //
21 #include "xoptions.h"
22
XOptions(QObject * pParent)23 XOptions::XOptions(QObject *pParent) : QObject(pParent)
24 {
25 g_bIsNeedRestart=false;
26 g_bIsNative=checkNative();
27
28 g_sName=QString("%1.ini").arg(qApp->applicationName()); // If setName not used
29 }
30
setValueIDs(QList<ID> listVariantIDs)31 void XOptions::setValueIDs(QList<ID> listVariantIDs)
32 {
33 int nNumberOfIds=listVariantIDs.count();
34
35 bool bSaveLastDirectory=false;
36 bool bLastDirectory=false;
37
38 for(int i=0;i<nNumberOfIds;i++)
39 {
40 if(listVariantIDs.at(i)==ID_SAVELASTDIRECTORY)
41 {
42 bSaveLastDirectory=true;
43 }
44
45 if(listVariantIDs.at(i)==ID_NU_LASTDIRECTORY)
46 {
47 bLastDirectory=true;
48 }
49 }
50
51 if(bSaveLastDirectory&&(!bLastDirectory))
52 {
53 listVariantIDs.append(ID_NU_LASTDIRECTORY);
54 }
55
56 this->g_listValueIDs=listVariantIDs;
57 }
58
setDefaultValues(QMap<XOptions::ID,QVariant> mapDefaultValues)59 void XOptions::setDefaultValues(QMap<XOptions::ID, QVariant> mapDefaultValues)
60 {
61 this->g_mapDefaultValues=mapDefaultValues;
62 }
63
setNative(bool bValue)64 void XOptions::setNative(bool bValue)
65 {
66 g_bIsNative=bValue;
67 }
68
isNative()69 bool XOptions::isNative()
70 {
71 return g_bIsNative;
72 }
73
isAppImage()74 bool XOptions::isAppImage()
75 {
76 // TODO more checks
77 bool bResult=false;
78
79 QString sApplicationDirPath=qApp->applicationDirPath();
80
81 bResult=(sApplicationDirPath.contains("/tmp/.mount_")); // TODO check
82
83 return bResult;
84 }
85
setName(QString sValue)86 void XOptions::setName(QString sValue)
87 {
88 g_sName=sValue;
89 }
90
load()91 void XOptions::load()
92 {
93 QSettings *pSettings=nullptr;
94
95 if(g_bIsNative)
96 {
97 pSettings=new QSettings;
98 }
99 else
100 {
101 pSettings=new QSettings(getApplicationDataPath()+QDir::separator()+QString("%1").arg(g_sName),QSettings::IniFormat);
102 }
103
104 #ifdef QT_DEBUG
105 if(pSettings)
106 {
107 qDebug("XOptions load %s",pSettings->fileName().toLatin1().data());
108 }
109 #endif
110
111 int nNumberOfIDs=g_listValueIDs.count();
112
113 for(int i=0;i<nNumberOfIDs;i++)
114 {
115 ID id=g_listValueIDs.at(i);
116 QString sName=idToString(id);
117
118 QVariant varDefault;
119
120 if(g_mapDefaultValues.contains(id))
121 {
122 varDefault=g_mapDefaultValues.value(id);
123 }
124 else
125 {
126 switch(id)
127 {
128 case ID_STAYONTOP: varDefault=false; break;
129 case ID_SCANAFTEROPEN: varDefault=true; break;
130 case ID_RECURSIVESCAN: varDefault=true; break;
131 case ID_DEEPSCAN: varDefault=true; break;
132 case ID_HEURISTICSCAN: varDefault=true; break;
133 case ID_SAVELASTDIRECTORY: varDefault=true; break;
134 case ID_NU_LASTDIRECTORY: varDefault=""; break;
135 case ID_SAVEBACKUP: varDefault=true; break;
136 case ID_STYLE: varDefault="Fusion"; break; // TODO Check OSX&Linux
137 case ID_LANG: varDefault="System"; break;
138 case ID_QSS: varDefault=""; break;
139 case ID_DATABASEPATH: varDefault="$data/db"; break;
140 case ID_INFOPATH: varDefault="$data/info"; break;
141 case ID_SCANENGINE: varDefault="die"; break;
142 case ID_ROOTPATH: varDefault=""; break;
143 case ID_DATAPATH: varDefault="$data/data"; break;
144 case ID_JSON: varDefault=""; break;
145 case ID_SINGLEAPPLICATION: varDefault=false; break;
146 case ID_SEARCHSIGNATURESPATH: varDefault="$data/signatures"; break;
147 case ID_AUTHUSER: varDefault=""; break;
148 case ID_AUTHTOKEN: varDefault=""; break;
149 case ID_SHOWLOGO: varDefault=true; break;
150 default: varDefault="";
151 }
152 }
153
154 QVariant variant=pSettings->value(sName,varDefault);
155
156 if(!variant.toString().contains("$data"))
157 {
158 if( (id==ID_DATABASEPATH)||
159 (id==ID_QSS)||
160 (id==ID_INFOPATH)||
161 (id==ID_DATAPATH)||
162 (id==ID_SEARCHSIGNATURESPATH))
163 {
164 if(!QDir(variant.toString()).exists())
165 {
166 variant=varDefault;
167 }
168 }
169 }
170
171 if(g_bIsNative)
172 {
173 if(variant.toString().contains("$data"))
174 {
175 QString sValue=variant.toString();
176 sValue=sValue.replace("$data",getApplicationDataPath());
177 variant=sValue;
178 }
179 }
180
181 g_mapValues.insert(id,variant);
182 }
183
184 QString sLastDirectory=g_mapValues.value(ID_NU_LASTDIRECTORY).toString();
185
186 if(sLastDirectory!="")
187 {
188 if(!QDir(sLastDirectory).exists())
189 {
190 g_mapValues.insert(ID_NU_LASTDIRECTORY,"");
191 }
192 }
193
194 delete pSettings;
195 }
196
save()197 void XOptions::save()
198 {
199 QSettings *pSettings=nullptr;
200
201 if(g_bIsNative)
202 {
203 pSettings=new QSettings;
204 }
205 else
206 {
207 pSettings=new QSettings(getApplicationDataPath()+QDir::separator()+QString("%1").arg(g_sName),QSettings::IniFormat);
208 }
209
210 #ifdef QT_DEBUG
211 if(pSettings)
212 {
213 qDebug("XOptions save %s",pSettings->fileName().toLatin1().data());
214 }
215 #endif
216
217 int nNumberOfIDs=g_listValueIDs.count();
218
219 for(int i=0;i<nNumberOfIDs;i++)
220 {
221 ID id=g_listValueIDs.at(i);
222 QString sName=idToString(id);
223 pSettings->setValue(sName,g_mapValues.value(id));
224 }
225
226 delete pSettings;
227 }
228
getValue(XOptions::ID id)229 QVariant XOptions::getValue(XOptions::ID id)
230 {
231 return g_mapValues.value(id);
232 }
233
setValue(XOptions::ID id,QVariant vValue)234 void XOptions::setValue(XOptions::ID id, QVariant vValue)
235 {
236 if( (id==ID_STYLE)||
237 (id==ID_LANG)||
238 (id==ID_QSS)||
239 (id==ID_SEARCHSIGNATURESPATH)) // TODO remove
240 {
241 QVariant vOld=g_mapValues.value(id);
242
243 if(vValue!=vOld)
244 {
245 g_bIsNeedRestart=true;
246 }
247 }
248
249 g_mapValues.insert(id,vValue);
250 }
251
clearValue(XOptions::ID id)252 void XOptions::clearValue(XOptions::ID id)
253 {
254 g_mapValues.insert(id,"");
255 }
256
idToString(ID id)257 QString XOptions::idToString(ID id)
258 {
259 QString sResult="Unknown";
260
261 switch(id)
262 {
263 case ID_STAYONTOP: sResult=QString("StayOnTop"); break;
264 case ID_SCANAFTEROPEN: sResult=QString("ScanAfterOpen"); break;
265 case ID_RECURSIVESCAN: sResult=QString("RecursiveScan"); break;
266 case ID_DEEPSCAN: sResult=QString("DeepScan"); break;
267 case ID_HEURISTICSCAN: sResult=QString("HeuristicScan"); break;
268 case ID_SAVELASTDIRECTORY: sResult=QString("SaveLastDirectory"); break;
269 case ID_NU_LASTDIRECTORY: sResult=QString("LastDirectory"); break;
270 case ID_SAVEBACKUP: sResult=QString("SaveBackup"); break;
271 case ID_STYLE: sResult=QString("Style"); break;
272 case ID_LANG: sResult=QString("Lang"); break;
273 case ID_QSS: sResult=QString("Qss"); break;
274 case ID_DATABASEPATH: sResult=QString("DatabasePath"); break;
275 case ID_INFOPATH: sResult=QString("InfoPath"); break;
276 case ID_SCANENGINE: sResult=QString("ScanEngine"); break;
277 case ID_ROOTPATH: sResult=QString("RootPath"); break;
278 case ID_DATAPATH: sResult=QString("DataPath"); break;
279 case ID_JSON: sResult=QString("Json"); break;
280 case ID_SINGLEAPPLICATION: sResult=QString("SingleApplication"); break;
281 case ID_SEARCHSIGNATURESPATH: sResult=QString("SearchSignaturesPath"); break;
282 case ID_AUTHUSER: sResult=QString("AuthUser"); break;
283 case ID_AUTHTOKEN: sResult=QString("AuthToken"); break;
284 case ID_SHOWLOGO: sResult=QString("ShowLogo"); break;
285 }
286
287 return sResult;
288 }
289
getLastDirectory()290 QString XOptions::getLastDirectory()
291 {
292 QString sResult;
293
294 bool bSaveLastDirectory=getValue(ID_SAVELASTDIRECTORY).toBool();
295 QString sLastDirectory=getValue(ID_NU_LASTDIRECTORY).toString();
296
297 if(bSaveLastDirectory&&(sLastDirectory!="")&&QDir().exists(sLastDirectory))
298 {
299 sResult=sLastDirectory;
300 }
301
302 return sResult;
303 }
304
setLastDirectory(QString sValue)305 void XOptions::setLastDirectory(QString sValue)
306 {
307 QFileInfo fi(sValue);
308
309 if(fi.isFile())
310 {
311 sValue=fi.absolutePath();
312 }
313 else if(fi.isDir())
314 {
315 sValue=fi.absoluteFilePath();
316 }
317
318 if(getValue(ID_SAVELASTDIRECTORY).toBool())
319 {
320 setValue(ID_NU_LASTDIRECTORY,sValue);
321 }
322 }
323
getDatabasePath()324 QString XOptions::getDatabasePath()
325 {
326 return getValue(ID_DATABASEPATH).toString();
327 }
328
getInfoPath()329 QString XOptions::getInfoPath()
330 {
331 return getValue(ID_INFOPATH).toString();
332 }
333
getScanEngine()334 QString XOptions::getScanEngine()
335 {
336 return getValue(ID_SCANENGINE).toString();
337 }
338
getRootPath()339 QString XOptions::getRootPath()
340 {
341 return getValue(ID_ROOTPATH).toString();
342 }
343
getDataPath()344 QString XOptions::getDataPath()
345 {
346 return getValue(ID_DATAPATH).toString();
347 }
348
getJson()349 QString XOptions::getJson()
350 {
351 return getValue(ID_JSON).toString();
352 }
353
getAuthUser()354 QString XOptions::getAuthUser()
355 {
356 return getValue(ID_AUTHUSER).toString();
357 }
358
getAuthToken()359 QString XOptions::getAuthToken()
360 {
361 return getValue(ID_AUTHTOKEN).toString();
362 }
363
364 #ifdef QT_GUI_LIB
adjustStayOnTop(QWidget * pWidget)365 void XOptions::adjustStayOnTop(QWidget *pWidget)
366 {
367 Qt::WindowFlags wf=pWidget->windowFlags();
368
369 if(isStayOnTop())
370 {
371 wf|=Qt::WindowStaysOnTopHint;
372 }
373 else
374 {
375 wf&=~(Qt::WindowStaysOnTopHint);
376 }
377
378 pWidget->setWindowFlags(wf);
379 pWidget->show();
380 }
381 #endif
382 #ifdef QT_GUI_LIB
setMonoFont(QWidget * pWidget,qint32 nSize)383 void XOptions::setMonoFont(QWidget *pWidget,qint32 nSize)
384 {
385 QFont font=pWidget->font();
386 font.setFamily("Courier"); // TODO OSX and Linux
387
388 #ifdef Q_OS_WIN
389 font.setFamily("Courier");
390 #endif
391 #ifdef Q_OS_LINUX
392 font.setFamily("Monospace");
393 #endif
394 #ifdef Q_OS_OSX
395 font.setFamily("Menlo");
396 #endif
397
398 if(nSize!=-1)
399 {
400 font.setPointSize(nSize);
401 }
402
403 pWidget->setFont(font);
404 }
405 #endif
406 #ifdef QT_GUI_LIB
setCheckBox(QCheckBox * pCheckBox,XOptions::ID id)407 void XOptions::setCheckBox(QCheckBox *pCheckBox, XOptions::ID id)
408 {
409 pCheckBox->setChecked(getValue(id).toBool());
410 }
411 #endif
412 #ifdef QT_GUI_LIB
getCheckBox(QCheckBox * pCheckBox,XOptions::ID id)413 void XOptions::getCheckBox(QCheckBox *pCheckBox, XOptions::ID id)
414 {
415 setValue(id,pCheckBox->isChecked());
416 }
417 #endif
418 #ifdef QT_GUI_LIB
setComboBox(QComboBox * pComboBox,XOptions::ID id)419 void XOptions::setComboBox(QComboBox *pComboBox, XOptions::ID id)
420 {
421 #if QT_VERSION >= 0x050300
422 const QSignalBlocker signalBlocker(pComboBox);
423 #else
424 const bool bBlocked1=pComboBox->blockSignals(true);
425 #endif
426
427 pComboBox->clear();
428
429 QString sValue=getValue(id).toString();
430
431 if(id==ID_STYLE)
432 {
433 pComboBox->addItem("Default","");
434 QStringList listKeys=QStyleFactory::keys();
435
436 int nNumberOfKeys=listKeys.count();
437
438 for(int i=0;i<nNumberOfKeys;i++)
439 {
440 QString sRecord=listKeys.at(i);
441 pComboBox->addItem(sRecord,sRecord);
442 }
443 }
444 else if(id==ID_LANG)
445 {
446 pComboBox->addItem("English","");
447 pComboBox->addItem("System","System");
448
449 QList<QString> listFileNames=getAllFilesFromDirectory(getApplicationLangPath(),"*.qm");
450
451 int nNumberOfRecords=listFileNames.count();
452
453 for(int i=0;i<nNumberOfRecords;i++)
454 {
455 QFileInfo fi(listFileNames.at(i));
456
457 QString sRecord=fi.baseName();
458
459 QLocale locale(sRecord.section("_",1,-1));
460 QString sLocale=locale.nativeLanguageName();
461
462 if(sRecord.count("_")==2)
463 {
464 sLocale+=QString("(%1)").arg(locale.nativeCountryName());
465 }
466
467 pComboBox->addItem(sLocale,sRecord);
468 }
469 }
470 else if(id==ID_QSS)
471 {
472 pComboBox->addItem("Default","");
473
474 QList<QString> listFileNames=getAllFilesFromDirectory(getApplicationQssPath(),"*.qss");
475
476 int nNumberOfRecords=listFileNames.count();
477
478 for(int i=0;i<nNumberOfRecords;i++)
479 {
480 QFileInfo fi(listFileNames.at(i));
481
482 QString sRecord=fi.baseName();
483
484 pComboBox->addItem(sRecord,sRecord);
485 }
486 }
487 else if(id==ID_SCANENGINE)
488 {
489 pComboBox->addItem(QString("Auto"),"auto");
490 pComboBox->addItem(QString("Detect It Easy(DiE)"),"die");
491 pComboBox->addItem(QString("Nauz File Detector(NFD)"),"nfd");
492 }
493
494 int nNumberOfItems=pComboBox->count();
495 int nIndex=-1;
496
497 for(int i=0;i<nNumberOfItems;i++)
498 {
499 if(sValue==pComboBox->itemData(i,Qt::UserRole))
500 {
501 nIndex=i;
502 }
503 }
504
505 if(nIndex!=-1)
506 {
507 pComboBox->setCurrentIndex(nIndex);
508 }
509
510 #if QT_VERSION < 0x050300
511 pComboBox->blockSignals(bBlocked1);
512 #endif
513 }
514 #endif
515 #ifdef QT_GUI_LIB
getComboBox(QComboBox * pComboBox,XOptions::ID id)516 void XOptions::getComboBox(QComboBox *pComboBox, XOptions::ID id)
517 {
518 setValue(id,pComboBox->currentData());
519 }
520 #endif
521 #ifdef QT_GUI_LIB
setLineEdit(QLineEdit * pLineEdit,XOptions::ID id)522 void XOptions::setLineEdit(QLineEdit *pLineEdit, XOptions::ID id)
523 {
524 pLineEdit->setText(getValue(id).toString());
525 }
526 #endif
527 #ifdef QT_GUI_LIB
getLineEdit(QLineEdit * pLineEdit,XOptions::ID id)528 void XOptions::getLineEdit(QLineEdit *pLineEdit, XOptions::ID id)
529 {
530 setValue(id,pLineEdit->text());
531 }
532 #endif
isSaveBackup()533 bool XOptions::isSaveBackup()
534 {
535 return getValue(XOptions::ID_SAVEBACKUP).toBool();
536 }
537
isSaveLastDirectory()538 bool XOptions::isSaveLastDirectory()
539 {
540 return getValue(XOptions::ID_SAVELASTDIRECTORY).toBool();
541 }
542
isRestartNeeded()543 bool XOptions::isRestartNeeded()
544 {
545 return g_bIsNeedRestart;
546 }
547
isStayOnTop()548 bool XOptions::isStayOnTop()
549 {
550 return getValue(XOptions::ID_STAYONTOP).toBool();
551 }
552
isScanAfterOpen()553 bool XOptions::isScanAfterOpen()
554 {
555 return getValue(XOptions::ID_SCANAFTEROPEN).toBool();
556 }
557
isRecursiveScan()558 bool XOptions::isRecursiveScan()
559 {
560 return getValue(XOptions::ID_RECURSIVESCAN).toBool();
561 }
562
isDeepScan()563 bool XOptions::isDeepScan()
564 {
565 return getValue(XOptions::ID_DEEPSCAN).toBool();
566 }
567
isHeuristicScan()568 bool XOptions::isHeuristicScan()
569 {
570 return getValue(XOptions::ID_HEURISTICSCAN).toBool();
571 }
572
isSingleApplication()573 bool XOptions::isSingleApplication()
574 {
575 return getValue(XOptions::ID_SINGLEAPPLICATION).toBool();
576 }
577
isShowLogo()578 bool XOptions::isShowLogo()
579 {
580 return getValue(XOptions::ID_SHOWLOGO).toBool();
581 }
582
getSearchSignaturesPath()583 QString XOptions::getSearchSignaturesPath()
584 {
585 return getValue(XOptions::ID_SEARCHSIGNATURESPATH).toString();
586 }
587 #ifdef QT_GUI_LIB
adjustApplicationView(QString sTranslationName,XOptions * pOptions)588 void XOptions::adjustApplicationView(QString sTranslationName, XOptions *pOptions)
589 {
590 QString sStyle=pOptions->getValue(XOptions::ID_STYLE).toString();
591
592 if(sStyle!="")
593 {
594 qApp->setStyle(QStyleFactory::create(sStyle));
595 }
596
597 QTranslator *pTranslator=new QTranslator; // Important
598 QString sLang=pOptions->getValue(XOptions::ID_LANG).toString();
599 QString sLangsPath=pOptions->getApplicationLangPath();
600
601 bool bLoad=false;
602
603 if(sLang=="System")
604 {
605 QLocale locale=QLocale::system();
606 if(locale!=QLocale::English)
607 {
608 bLoad=pTranslator->load(locale,sTranslationName,"_",sLangsPath,".qm");
609 }
610 }
611 else if(sLang!="")
612 {
613 bLoad=pTranslator->load(sLang,sLangsPath);
614 }
615
616 if(bLoad)
617 {
618 qApp->installTranslator(pTranslator);
619 }
620
621 QString sQss=pOptions->getValue(XOptions::ID_QSS).toString();
622
623 if(sQss!="")
624 {
625 QString sQssFileName=pOptions->getApplicationQssPath()+QDir::separator()+QString("%1.qss").arg(sQss);
626
627 if(QFile::exists(sQssFileName))
628 {
629 QFile file;
630 file.setFileName(sQssFileName);
631
632 if(file.open(QIODevice::ReadOnly))
633 {
634 QByteArray baQss=file.readAll();
635 qApp->setStyleSheet(baQss.data());
636 file.close();
637 }
638 }
639 }
640 }
641 #endif
642 #ifdef QT_GUI_LIB
adjustApplicationView(QString sApplicationFileName,QString sTranslationName)643 void XOptions::adjustApplicationView(QString sApplicationFileName,QString sTranslationName)
644 {
645 XOptions xOptions;
646
647 xOptions.setName(sApplicationFileName);
648
649 QList<XOptions::ID> listIDs;
650
651 listIDs.append(XOptions::ID_STYLE);
652 listIDs.append(XOptions::ID_LANG);
653 listIDs.append(XOptions::ID_QSS);
654
655 xOptions.setValueIDs(listIDs);
656 xOptions.load();
657
658 xOptions.adjustApplicationView(sTranslationName,&xOptions);
659 }
660 #endif
661 #ifdef QT_GUI_LIB
getMainWidget(QWidget * pWidget)662 QWidget *XOptions::getMainWidget(QWidget *pWidget)
663 {
664 QWidget *pResult=pWidget;
665
666 while(pResult->parent())
667 {
668 pResult=qobject_cast<QWidget *>(pResult->parent());
669 }
670
671 return pResult;
672 }
673 #endif
674 #ifdef QT_GUI_LIB
saveTable(QAbstractItemModel * pModel,QString sFileName)675 bool XOptions::saveTable(QAbstractItemModel *pModel,QString sFileName)
676 {
677 bool bResult=false;
678
679 QFile file;
680 file.setFileName(sFileName);
681
682 if(file.open(QIODevice::ReadWrite))
683 {
684 int nNumberOfRows=pModel->rowCount();
685 int nNumberOfColumns=pModel->columnCount();
686
687 QString sResult;
688
689 for(int i=0; i<nNumberOfRows; i++)
690 {
691 for(int j=0;j<nNumberOfColumns;j++)
692 {
693 QString sText=pModel->data(pModel->index(i,j)).toString();
694
695 if(j!=(nNumberOfColumns-1))
696 {
697 sResult+=QString("%1\t").arg(sText);
698 }
699 else
700 {
701 sResult+=QString("%1\r\n").arg(sText);
702 }
703 }
704 }
705
706 file.resize(0);
707 file.write(sResult.toLatin1().data());
708
709 file.close();
710
711 bResult=true;
712 }
713
714 return bResult;
715 }
716 #endif
getApplicationLangPath()717 QString XOptions::getApplicationLangPath()
718 {
719 QString sResult;
720
721 sResult=getApplicationDataPath()+QDir::separator()+"lang";
722
723 return sResult;
724 }
725
getApplicationQssPath()726 QString XOptions::getApplicationQssPath()
727 {
728 QString sResult;
729
730 sResult=getApplicationDataPath()+QDir::separator()+"qss";
731
732 return sResult;
733 }
734
getAllFilesFromDirectory(QString sDirectory,QString sExtension)735 QList<QString> XOptions::getAllFilesFromDirectory(QString sDirectory, QString sExtension)
736 {
737 QDir directory(sDirectory);
738
739 return directory.entryList(QStringList()<<sExtension,QDir::Files);
740 }
741
checkNative()742 bool XOptions::checkNative()
743 {
744 bool bResult=false;
745 #if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD)
746 bResult=true;
747 #elif defined(Q_OS_LINUX)
748 QString sApplicationDirPath=qApp->applicationDirPath();
749
750 if( (sApplicationDirPath=="/bin")||
751 (sApplicationDirPath=="/usr/bin")||
752 (sApplicationDirPath=="/usr/local/bin")||
753 isAppImage())
754 {
755 bResult=true;
756 }
757 else
758 {
759 bResult=false;
760 }
761 #elif defined(Q_OS_WIN)
762 QString sApplicationDirPath=qApp->applicationDirPath();
763
764 if(sApplicationDirPath.contains("C:\\Program Files\\")||sApplicationDirPath.contains("C:\\Program Files (x86)\\"))
765 {
766 bResult=true;
767 }
768 #endif
769
770 return bResult;
771 }
772
getApplicationDataPath()773 QString XOptions::getApplicationDataPath()
774 {
775 QString sResult;
776
777 QString sApplicationDirPath=qApp->applicationDirPath();
778
779 #ifdef Q_OS_MAC
780 sResult=sApplicationDirPath+"/../Resources";
781 #elif defined(Q_OS_LINUX)
782 if(g_bIsNative)
783 {
784 if(sApplicationDirPath.contains("/tmp/.mount_")) // AppImage
785 {
786 sResult=sApplicationDirPath.section("/",0,2);
787 }
788
789 sResult+=QString("/usr/lib/%1").arg(qApp->applicationName());
790 }
791 else
792 {
793 sResult=sApplicationDirPath;
794 }
795 #elif defined(Q_OS_FREEBSD)
796 sResult = QStandardPaths::standardLocations(
797 QStandardPaths::GenericDataLocation).at(1)
798 + QDir::separator() + qApp->applicationName();
799 #else
800 sResult=sApplicationDirPath;
801 #endif
802
803 return sResult;
804 }
805
806 #ifdef Q_OS_WIN
registerContext(QString sApplicationName,QString sType,QString sApplicationFilePath)807 bool XOptions::registerContext(QString sApplicationName, QString sType, QString sApplicationFilePath)
808 {
809 QSettings settings(QString("HKEY_CLASSES_ROOT\\%1\\shell\\%2\\command").arg(sType,sApplicationName),QSettings::NativeFormat);
810 settings.setValue(".","\""+sApplicationFilePath.replace("/","\\")+"\" \"%1\"");
811
812 QSettings settingsIcon(QString("HKEY_CLASSES_ROOT\\%1\\shell\\%2").arg(sType,sApplicationName),QSettings::NativeFormat);
813 settingsIcon.setValue("Icon","\""+sApplicationFilePath.replace("/","\\")+"\"");
814
815 // TODO Check if not send message
816 return checkContext(sApplicationName,sType);
817 }
818 #endif
819 #ifdef Q_OS_WIN
clearContext(QString sApplicationName,QString sType)820 bool XOptions::clearContext(QString sApplicationName, QString sType)
821 {
822 QSettings settings(QString("HKEY_CLASSES_ROOT\\%1\\shell\\%2").arg(sType,sApplicationName),QSettings::NativeFormat);
823 settings.clear();
824
825 // TODO Check if not send message
826 return !(checkContext(sApplicationName,sType));
827 }
828 #endif
829 #ifdef Q_OS_WIN
checkContext(QString sApplicationName,QString sType)830 bool XOptions::checkContext(QString sApplicationName, QString sType)
831 {
832 QSettings settings(QString("HKEY_CLASSES_ROOT\\%1\\shell").arg(sType),QSettings::NativeFormat);
833
834 return (settings.value(QString("%1/command/Default").arg(sApplicationName)).toString()!="");
835 }
836 #endif
837