1 /**********************************************************************************************
2    Copyright (C) 2015 Ivo Kronenberg <>
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #include "setup/CAppSetupMac.h"
20 
21 QString CAppSetupMac::relTranslationDir = "Resources/translations"; // app
22 QString CAppSetupMac::relRoutinoDir = "Resources/routino";     // app
23 QString CAppSetupMac::relGdalDir = "Resources/gdal";        // app
24 QString CAppSetupMac::relProjDir = "Resources/proj";        // app
25 QString CAppSetupMac::relHelpDir = "Resources/help";        // app
26 QString CAppSetupMac::relBinDir = "Tools";         // app
27 QString CAppSetupMac::relLogDir = "Library/Logs";         // home
28 
29 
extendPath()30 void CAppSetupMac::extendPath()
31 {
32     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
33     QStringList envlist = env.toStringList();
34     QString value = "";
35     for(int i = 0; i < envlist.size(); i++)
36     {
37         QString entry = envlist[i];
38         if(entry.startsWith("PATH="))
39         {
40             int index = entry.indexOf("=");
41 
42             if(index != -1)
43             {
44                 value = entry.right(entry.length() - (index + 1)) + ":";
45             }
46             break;
47         }
48     }
49     QString binDir = getApplicationDir(relBinDir).absolutePath();
50     qDebug() << "BIN" << binDir;
51     value += binDir;
52     qputenv("PATH", value.toLatin1().constData());
53 }
54 
55 
initQMapShack()56 void CAppSetupMac::initQMapShack()
57 {
58     extendPath();
59     // setup gdal
60     QString gdalDir = getApplicationDir(relGdalDir).absolutePath();
61     QString projDir = getApplicationDir(relProjDir).absolutePath();
62     prepareGdal(gdalDir, projDir);
63 
64     // setup translators
65     QString translationPath = getApplicationDir(relTranslationDir).absolutePath();
66     prepareTranslator(translationPath, "qt_");
67     prepareTranslator(translationPath, "qmapshack_");
68 
69     // load and apply style sheet
70     QApplication* app = (QApplication*) QCoreApplication::instance();
71 
72     QString fileName = QDir(getApplicationDir("Resources")).absoluteFilePath("qms-style.qss");
73     qDebug() << "Stylesheet" << fileName;
74     QFile styleFile(fileName);
75     styleFile.open(QFile::ReadOnly);
76     QString style(QLatin1String(styleFile.readAll()));
77     app->setStyleSheet(style);
78 
79     migrateDirContent(defaultCachePath());
80     migrateDirContent(userDataPath());
81 
82     // create directories
83     IAppSetup::path(defaultCachePath(), 0, true, "CACHE");
84     IAppSetup::path(userDataPath("WaypointIcons"), 0, true, "USER DATA");
85     IAppSetup::path(logDir(), 0, false, "LOG");
86 }
87 
88 
routinoPath(QString xmlFile)89 QString CAppSetupMac::routinoPath(QString xmlFile)
90 {
91     QDir dirXml = getApplicationDir(relRoutinoDir);
92     return IAppSetup::path(dirXml.absolutePath(), xmlFile, false, "ROUTINO");
93 }
94 
95 
defaultCachePath()96 QString CAppSetupMac::defaultCachePath()
97 {
98     const QStringList& standardLocations = QStandardPaths::standardLocations(QStandardPaths::CacheLocation);
99     const QString& cachePath = standardLocations.first();
100     return IAppSetup::path(cachePath, 0, false, 0);
101 }
102 
103 
userDataPath(QString subdir)104 QString CAppSetupMac::userDataPath(QString subdir)
105 {
106 #if QT_VERSION >= 0x050400
107     const QStringList& standardLocations = QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation);
108     const QString& dataDir = standardLocations.first();
109 #else
110     const QStringList& standardLocations = QStandardPaths::standardLocations(QStandardPaths::DataLocation);
111     const QString& dataDir = standardLocations.first();
112 #endif
113     return IAppSetup::path(dataDir, subdir, false, 0);
114 }
115 
116 
logDir()117 QString CAppSetupMac::logDir()
118 {
119     // home location returns / (root) instead of user home...
120     const QStringList& standardLocations = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
121     const QString& home = standardLocations.first();
122     QDir dir = QDir(home);
123     dir.cdUp();
124     return IAppSetup::path(dir.absolutePath(), relLogDir, false, 0);
125 }
126 
127 
getApplicationDir(QString subdir)128 QDir CAppSetupMac::getApplicationDir(QString subdir)
129 {
130     QDir appDir(QCoreApplication::applicationDirPath());
131     appDir.cdUp();
132     appDir.cd(subdir);
133     return appDir;
134 }
135 
136 
migrateDirContent(QString dest)137 void CAppSetupMac::migrateDirContent(QString dest)
138 {
139     QString src = dest;
140     src.replace("/QLandkarte/", "/");
141     QDir dirDest = QDir(dest);
142     QDir dirSource = QDir(src);
143 
144     if (!dirDest.exists() && dirSource.exists())
145     {
146         qDebug() << "src directory for migration" << src;
147         qDebug() << "dst directory for migration" << dest;
148 
149         QDir wdir;
150         QString newdir = dest;
151         newdir.remove("/QMapShack");
152         wdir.mkdir(newdir);
153         qDebug() << "directory created" << newdir;
154 
155         qDebug() << "migrate data from " << dirSource.absolutePath() << "to" << dirDest.absolutePath();
156         QDir mvDir;
157         if(!mvDir.rename(dirSource.absolutePath(), dirDest.absolutePath()))
158         {
159             qDebug() << "error migrating directory" << dirSource;
160         }
161     }
162 }
163 
helpFile()164 QString CAppSetupMac::helpFile()
165 {
166     QDir dirHelp(getApplicationDir(relHelpDir));
167     return dirHelp.absoluteFilePath("QMSHelp.qhc");
168 }
169