Initial commit
[keepassx] / src / main_unix.cpp
1 /***************************************************************************
2  *   Copyright (C) 1992-2008 Trolltech ASA                                                                 *
3  *                                                                         *
4  *   Copyright (C) 2005-2008 by Tarek Saidi                                *
5  *   tarek.saidi@arcor.de                                                  *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; version 2 of the License.               *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20  ***************************************************************************/
21
22
23 #include "main.h"
24
25 void initAppPaths(int argc,char** argv) {
26         // Try looking for a /proc/<pid>/exe symlink first which points to
27         // the absolute path of the executable
28         QFileInfo pfi(QString::fromLatin1("/proc/%1/exe").arg(getpid()));
29         if (pfi.exists() && pfi.isSymLink()) {
30                 AppDir = pfi.canonicalFilePath();
31         }
32         else {  
33                 QString argv0 = QFile::decodeName(QByteArray(argv[0]));
34                 QString absPath;
35                 
36                 if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) {
37                                 /*
38                                 If argv0 starts with a slash, it is already an absolute
39                                 file path.
40                                 */
41                         absPath = argv0;
42                 } else if (argv0.contains(QLatin1Char('/'))) {
43                                 /*
44                                 If argv0 contains one or more slashes, it is a file path
45                                 relative to the current directory.
46                                 */
47                         absPath = QDir::current().absoluteFilePath(argv0);
48                 } else {
49                                 /*
50                                 Otherwise, the file path has to be determined using the
51                                 PATH environment variable.
52                                 */
53                         QByteArray pEnv = qgetenv("PATH");
54                         QDir currentDir = QDir::current();
55                         QStringList paths = QString::fromLocal8Bit(pEnv.constData()).split(QLatin1String(":"));
56                         for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) {
57                                 if ((*p).isEmpty())
58                                         continue;
59                                 QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0);
60                                 QFileInfo candidate_fi(candidate);
61                                 if (candidate_fi.exists() && !candidate_fi.isDir()) {
62                                         absPath = candidate;
63                                         break;
64                                 }
65                         }
66                 }               
67                 absPath = QDir::cleanPath(absPath);             
68                 QFileInfo fi(absPath);
69                 AppDir = fi.exists() ? fi.canonicalFilePath() : QString();
70         }
71         AppDir.truncate(AppDir.lastIndexOf("/"));
72         DataDir=AppDir+"/../share/keepassx";
73         if (!QFile::exists(DataDir) && QFile::exists(AppDir+"/share"))
74                 DataDir=AppDir+"/share";
75         HomeDir = QDir::homePath()+"/.keepassx";
76 }