diff --git a/src/KpxConfig.cpp b/src/KpxConfig.cpp index 24fcc9d..55292c9 100644 --- a/src/KpxConfig.cpp +++ b/src/KpxConfig.cpp @@ -26,6 +26,7 @@ #include #include #include +#include KpxConfig::KpxConfig(const QString& filePath) : settings(filePath,QSettings::IniFormat){ configFile=filePath; diff --git a/src/KpxConfig.h b/src/KpxConfig.h index 39c51a9..97363b6 100644 --- a/src/KpxConfig.h +++ b/src/KpxConfig.h @@ -23,7 +23,7 @@ #ifndef _KPXCONFIG_H_ #define _KPXCONFIG_H_ -#include "main.h" +#include "lib/tools.h" #include "AutoType.h" #include #include diff --git a/src/dialogs/AboutDlg.cpp b/src/dialogs/AboutDlg.cpp index be1c3cd..a3723f4 100644 --- a/src/dialogs/AboutDlg.cpp +++ b/src/dialogs/AboutDlg.cpp @@ -19,7 +19,8 @@ #include #include - +#include +#include "lib/tools.h" #include "AboutDlg.h" diff --git a/src/dialogs/AddBookmarkDlg.cpp b/src/dialogs/AddBookmarkDlg.cpp index 82276db..ace4afe 100644 --- a/src/dialogs/AddBookmarkDlg.cpp +++ b/src/dialogs/AddBookmarkDlg.cpp @@ -19,6 +19,7 @@ #include #include +#include "lib/tools.h" #include "lib/FileDialogs.h" #include "lib/bookmarks.h" diff --git a/src/dialogs/AutoTypeDlg.cpp b/src/dialogs/AutoTypeDlg.cpp index 8b7bbe1..b32e522 100644 --- a/src/dialogs/AutoTypeDlg.cpp +++ b/src/dialogs/AutoTypeDlg.cpp @@ -17,6 +17,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "main.h" #include "AutoTypeDlg.h" #include "KpxConfig.h" #include diff --git a/src/dialogs/CalendarDlg.cpp b/src/dialogs/CalendarDlg.cpp index d6c3f18..3216cc0 100644 --- a/src/dialogs/CalendarDlg.cpp +++ b/src/dialogs/CalendarDlg.cpp @@ -18,6 +18,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "lib/tools.h" #include "main.h" #include "CalendarDlg.h" diff --git a/src/dialogs/CollectEntropyDlg.cpp b/src/dialogs/CollectEntropyDlg.cpp index 841f243..208654a 100644 --- a/src/dialogs/CollectEntropyDlg.cpp +++ b/src/dialogs/CollectEntropyDlg.cpp @@ -20,6 +20,9 @@ #include #include +#include +#include "lib/tools.h" + #include "crypto/yarrow.h" #include "CollectEntropyDlg.h" diff --git a/src/dialogs/DatabaseSettingsDlg.cpp b/src/dialogs/DatabaseSettingsDlg.cpp index 107e5b5..2275423 100644 --- a/src/dialogs/DatabaseSettingsDlg.cpp +++ b/src/dialogs/DatabaseSettingsDlg.cpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include "lib/tools.h" #include "main.h" #include "DatabaseSettingsDlg.h" diff --git a/src/dialogs/ExpiredEntriesDlg.cpp b/src/dialogs/ExpiredEntriesDlg.cpp index fc16507..39c8d46 100644 --- a/src/dialogs/ExpiredEntriesDlg.cpp +++ b/src/dialogs/ExpiredEntriesDlg.cpp @@ -22,6 +22,7 @@ #include #include #include +#include "lib/tools.h" #include "main.h" #include "ExpiredEntriesDlg.h" diff --git a/src/dialogs/ManageBookmarksDlg.cpp b/src/dialogs/ManageBookmarksDlg.cpp index 192c9ad..615cfc6 100644 --- a/src/dialogs/ManageBookmarksDlg.cpp +++ b/src/dialogs/ManageBookmarksDlg.cpp @@ -19,6 +19,7 @@ #include #include +#include "lib/tools.h" #include "ManageBookmarksDlg.h" #include "lib/bookmarks.h" #include "dialogs/AddBookmarkDlg.h" diff --git a/src/dialogs/PasswordDlg.h b/src/dialogs/PasswordDlg.h index 372aa42..d8bef29 100644 --- a/src/dialogs/PasswordDlg.h +++ b/src/dialogs/PasswordDlg.h @@ -21,6 +21,7 @@ #define PASSWORDDIALOG_H #include "ui_PasswordDlg.h" #include "main.h" +#include "lib/tools.h" #include "lib/UrlLabel.h" #include "Database.h" #include diff --git a/src/dialogs/SelectIconDlg.cpp b/src/dialogs/SelectIconDlg.cpp index b3dc6fe..13064c7 100644 --- a/src/dialogs/SelectIconDlg.cpp +++ b/src/dialogs/SelectIconDlg.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include "lib/tools.h" #include "SelectIconDlg.h" diff --git a/src/export/Export.cpp b/src/export/Export.cpp index 8dac57d..97b72f4 100644 --- a/src/export/Export.cpp +++ b/src/export/Export.cpp @@ -19,6 +19,8 @@ ***************************************************************************/ #include +#include +#include "lib/tools.h" #include "main.h" #include "Export.h" #include "lib/FileDialogs.h" diff --git a/src/import/Import.cpp b/src/import/Import.cpp index 7a12ca0..f689d2e 100644 --- a/src/import/Import.cpp +++ b/src/import/Import.cpp @@ -19,6 +19,8 @@ ***************************************************************************/ #include +#include +#include "lib/tools.h" #include "main.h" #include "Import.h" #include "lib/FileDialogs.h" diff --git a/src/lib/AutoType_X11.cpp b/src/lib/AutoType_X11.cpp index 078dc0b..7cfd66f 100644 --- a/src/lib/AutoType_X11.cpp +++ b/src/lib/AutoType_X11.cpp @@ -17,7 +17,8 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ - + +#include #include "KpxConfig.h" #include "AutoType.h" #include "mainwindow.h" diff --git a/src/lib/EntryView.cpp b/src/lib/EntryView.cpp index 7fbcf32..d6c7af3 100644 --- a/src/lib/EntryView.cpp +++ b/src/lib/EntryView.cpp @@ -35,6 +35,7 @@ #include "EntryView.h" #include "dialogs/EditEntryDlg.h" #include "lib/AutoType.h" +#include "Database.h" // just for the lessThan funtion QList* pItems; diff --git a/src/lib/UrlLabel.cpp b/src/lib/UrlLabel.cpp index 8ff720c..0408ecd 100644 --- a/src/lib/UrlLabel.cpp +++ b/src/lib/UrlLabel.cpp @@ -21,6 +21,7 @@ #include "UrlLabel.h" #include "main.h" +#include "lib/tools.h" #include #include #include diff --git a/src/lib/tools.cpp b/src/lib/tools.cpp new file mode 100644 index 0000000..6d91e34 --- /dev/null +++ b/src/lib/tools.cpp @@ -0,0 +1,179 @@ +/*************************************************************************** + * Copyright (C) 2005-2008 by Tarek Saidi * + * tarek.saidi@arcor.de * + * * + * 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; version 2 of the License. * + * * + * 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., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include +#include +#include "KpxConfig.h" +#include "main.h" +#include "lib/tools.h" + + +void createBanner(QPixmap* Pixmap,const QPixmap* IconAlpha,const QString& Text,int Width){ + createBanner(Pixmap,IconAlpha,Text,Width,config->bannerColor1(),config->bannerColor2(),config->bannerTextColor()); +} + +void createBanner(QPixmap* Pixmap,const QPixmap* IconAlpha,const QString& Text,int Width, QColor Color1, QColor Color2, QColor TextColor){ + *Pixmap=QPixmap(Width,50); + QPainter painter(Pixmap); + QLinearGradient grad(0,0,Width,0); + grad.setColorAt(0,Color1); + grad.setColorAt(1,Color2); + painter.setPen(Qt::NoPen); + painter.setBrush(grad); + painter.drawRect(0,0,Width,50); + + QPixmap Icon(32,32); + if(IconAlpha){ + Icon.fill(TextColor); + Icon.setAlphaChannel(*IconAlpha); + painter.drawPixmap(10,10,Icon); + } + + painter.setPen(QPen(TextColor)); + painter.setFont(QFont(QApplication::font().family(),16)); + painter.drawText(50,35,Text); +} + +QString decodeFileError(QFile::FileError Code){ + switch(Code){ + case QFile::NoError: return QApplication::translate("FileErrors","No error occurred."); + case QFile::ReadError: return QApplication::translate("FileErrors","An error occurred while reading from the file."); + case QFile::WriteError: return QApplication::translate("FileErrors","An error occurred while writing to the file."); + case QFile::FatalError: return QApplication::translate("FileErrors","A fatal error occurred."); + case QFile::ResourceError: return QApplication::translate("FileErrors","An resource error occurred."); + case QFile::OpenError: return QApplication::translate("FileErrors","The file could not be opened."); + case QFile::AbortError: return QApplication::translate("FileErrors","The operation was aborted."); + case QFile::TimeOutError: return QApplication::translate("FileErrors","A timeout occurred."); + case QFile::UnspecifiedError: return QApplication::translate("FileErrors","An unspecified error occurred."); + case QFile::RemoveError: return QApplication::translate("FileErrors","The file could not be removed."); + case QFile::RenameError: return QApplication::translate("FileErrors","The file could not be renamed."); + case QFile::PositionError: return QApplication::translate("FileErrors","The position in the file could not be changed."); + case QFile::ResizeError: return QApplication::translate("FileErrors","The file could not be resized."); + case QFile::PermissionsError: return QApplication::translate("FileErrors","The file could not be accessed."); + case QFile::CopyError: return QApplication::translate("FileErrors","The file could not be copied."); + } + return QString(); +} + +void openBrowser(IEntryHandle* entry){ + QString url = entry->url(); + url.replace("{TITLE}", entry->title(), Qt::CaseInsensitive); + url.replace("{USERNAME}", entry->username(), Qt::CaseInsensitive); + + if (url.contains("{PASSWORD}",Qt::CaseInsensitive)){ + SecString password=entry->password(); + password.unlock(); + url.replace("{PASSWORD}", password, Qt::CaseInsensitive); + } + + openBrowser(url); +} + +void openBrowser(const QString& UrlString){ + if (UrlString.startsWith("cmd://") && UrlString.length()>6){ + QProcess::startDetached(UrlString.right(UrlString.length()-6)); + return; + } + + QUrl url(UrlString); + if(url.scheme().isEmpty()) + url=QUrl("http://"+UrlString); + if(config->urlCmdDef() || url.scheme()=="mailto"){ + QDesktopServices::openUrl(url); + } + else{ + QByteArray UrlEncoded = url.toEncoded(); + QString browser = config->urlCmd(); + if (browser.contains("%u", Qt::CaseInsensitive)) + browser.replace("%u", UrlEncoded, Qt::CaseInsensitive); + else if (browser.contains("%1")) + browser.replace("%1", UrlEncoded); + else + browser.append(" ").append(UrlEncoded); + QProcess::startDetached(browser); + } +} + + +QString makePathRelative(const QString& AbsDir,const QString& CurDir){ + QStringList abs=AbsDir.split('/'); + QStringList cur=CurDir.split('/'); + QString rel="./"; + int common; + for(common=0; common < abs.size() && common < cur.size(); common++){ + if(abs[common]!=cur[common])break; + } + for(int i=0;iIconCache; + QIcon* CachedIcon=IconCache.value(name); + if(CachedIcon) + return *CachedIcon; + QIcon* NewIcon=NULL; + //TODO plugins + /*if(IconLoader){ + NewIcon=new QIcon(IconLoader->getIcon(name)); + if(NewIcon->isNull()){ + delete NewIcon; + NewIcon=NULL; + } + else + IconCache.insert(name,NewIcon); + }*/ + if(!NewIcon) + { + NewIcon=new QIcon(getImageFile(name+".png")); + IconCache.insert(name,NewIcon); + } + return *NewIcon; +} + +const QPixmap* getPixmap(const QString& name){ + static QHashPixmapCache; + QPixmap* CachedPixmap=PixmapCache.value(name); + if(CachedPixmap) + return CachedPixmap; + QImage img(getImageFile(name+".png")); + QPixmap* NewPixmap=new QPixmap(QPixmap::fromImage(img)); + PixmapCache.insert(name,NewPixmap); + return NewPixmap; +} + + diff --git a/src/lib/tools.h b/src/lib/tools.h new file mode 100644 index 0000000..fcfb54a --- /dev/null +++ b/src/lib/tools.h @@ -0,0 +1,41 @@ +/*************************************************************************** + * Copyright (C) 2005-2008 by Tarek Saidi * + * tarek.saidi@arcor.de * + * * + * 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; version 2 of the License. * + * * + * 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., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifndef TOOLS_H +#define TOOLS_H + +#include +#include +#include +#include + +#define CSTR(x)(x.toLocal8Bit().constData()) +class IEntryHandle; +typedef enum tKeyType {PASSWORD=0,KEYFILE=1,BOTH=2}; +const QIcon& getIcon(const QString& name); +const QPixmap* getPixmap(const QString& name); +void createBanner(QPixmap* Pixmap, const QPixmap* IconAlpha,const QString& Text,int Width); +void createBanner(QPixmap* Pixmap, const QPixmap* IconAlpha,const QString& Text,int Width, QColor Color1, QColor Color2, QColor TextColor); +void openBrowser(const QString& UrlString); +void openBrowser(IEntryHandle* entry); +void showErrMsg(const QString& msg,QWidget* parent=NULL); +QString decodeFileError(QFile::FileError Code); +QString makePathRelative(const QString& Abs,const QString& Cur); +QString getImageFile(const QString& name); + +#endif //TOOLS_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8d1b15c..13f3554 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,25 +19,11 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include -#include -#include -#include +#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - +#include /* -#include -#include #include "plugins/interfaces/IFileDialog.h" #include "plugins/interfaces/IKdeInit.h" #include "plugins/interfaces/IGnomeInit.h" @@ -53,20 +39,11 @@ #include "mainwindow.h" #include "crypto/yarrow.h" #if defined(Q_WS_X11) && defined(GLOBAL_AUTOTYPE) -#include "Application_X11.h" -#endif -#ifdef Q_WS_WIN -#include -#endif -#ifdef Q_WS_MAC -#include + #include "Application_X11.h" #endif using namespace std; -QHashPixmapCache; -QHashIconCache; - KpxConfig *config; QString AppDir; QString HomeDir; @@ -77,42 +54,25 @@ QString DetailViewTemplate; QPixmap* EntryIcons; //IIconTheme* IconLoader=NULL; //TODO plugins -char ** argv; inline void loadImages(); inline void parseCmdLineArgs(int argc, char** argv,QString &ArgFile,QString& ArgCfg,QString& ArgLang,bool& ArgMin,bool& ArgLock); bool loadTranslation(QTranslator* tr,const QString& prefix,const QString& LocaleCode,const QStringList& SearchPaths); +void initAppPaths(int argc, char **argv); -int main(int argc, char **_argv) +int main(int argc, char **argv) { - argv=_argv; - QString ArgFile,ArgCfg,ArgLang,IniFilename; - QApplication* app=NULL; - AppDir=applicationDirPath(); -#if defined Q_WS_WIN - HomeDir = QString::fromLocal8Bit(qgetenv("APPDATA").constData()); - if(!HomeDir.isEmpty() && QFile::exists(HomeDir)) - HomeDir = QDir::fromNativeSeparators(HomeDir)+"/KeePassX"; - else - HomeDir = QDir::homePath()+"/KeePassX"; -#else - HomeDir = QDir::homePath()+"/.keepassx"; -#endif - -#if defined Q_WS_MACX - DataDir=AppDir+"/../Resources/keepassx"; -#elif defined Q_WS_WIN - DataDir=AppDir+"/share"; -#else - DataDir=AppDir+"/../share/keepassx"; -#endif - bool ArgMin = false; - bool ArgLock = false; - parseCmdLineArgs(argc,argv,ArgFile,ArgCfg,ArgLang,ArgMin,ArgLock); - qDebug(CSTR(QDir::current().absolutePath())); - + QApplication* app=new QApplication(argc,argv); + initAppPaths(argc,argv); + CmdLineArgs args; + args.parse(app->arguments()); + delete app; + app=NULL; + qDebug(CSTR(AppDir)); + qDebug(CSTR(DataDir)); //Load Config - if(ArgCfg.isEmpty()){ + QString IniFilename; + if(args.configLocation().isEmpty()){ if(!QDir(HomeDir).exists()){ QDir conf(QDir::homePath()); if(!QDir().mkpath(HomeDir)) @@ -121,8 +81,7 @@ int main(int argc, char **_argv) IniFilename=HomeDir+"/config"; } else - IniFilename=ArgCfg; - + IniFilename=args.configLocation(); config = new KpxConfig(IniFilename); fileDlgHistory.load(); @@ -180,10 +139,10 @@ int main(int argc, char **_argv) //Internationalization QLocale loc; - if(!ArgLang.size()) + if(!args.language().size()) loc=QLocale::system(); else - loc=QLocale(ArgLang); + loc=QLocale(args.language()); QTranslator* translator = NULL; QTranslator* qtTranslator=NULL; @@ -221,7 +180,6 @@ int main(int argc, char **_argv) } } - DetailViewTemplate=config->detailViewTemplate(); loadImages(); @@ -230,7 +188,7 @@ int main(int argc, char **_argv) SecString::generateSessionKey(); QApplication::setQuitOnLastWindowClosed(false); - KeepassMainWindow *mainWin = new KeepassMainWindow(ArgFile,ArgMin,ArgLock); + KeepassMainWindow *mainWin = new KeepassMainWindow(args.file(), args.startMinimized(), args.startLocked()); int r=app->exec(); delete mainWin; @@ -241,103 +199,6 @@ int main(int argc, char **_argv) } -void createBanner(QPixmap* Pixmap,const QPixmap* IconAlpha,const QString& Text,int Width){ - createBanner(Pixmap,IconAlpha,Text,Width,config->bannerColor1(),config->bannerColor2(),config->bannerTextColor()); -} - -void createBanner(QPixmap* Pixmap,const QPixmap* IconAlpha,const QString& Text,int Width, QColor Color1, QColor Color2, QColor TextColor){ - *Pixmap=QPixmap(Width,50); - QPainter painter(Pixmap); - QLinearGradient grad(0,0,Width,0); - grad.setColorAt(0,Color1); - grad.setColorAt(1,Color2); - painter.setPen(Qt::NoPen); - painter.setBrush(grad); - painter.drawRect(0,0,Width,50); - - QPixmap Icon(32,32); - if(IconAlpha){ - Icon.fill(TextColor); - Icon.setAlphaChannel(*IconAlpha); - painter.drawPixmap(10,10,Icon); - } - - painter.setPen(QPen(TextColor)); - painter.setFont(QFont(QApplication::font().family(),16)); - painter.drawText(50,35,Text); -} - -QString decodeFileError(QFile::FileError Code){ - switch(Code){ - case QFile::NoError: return QApplication::translate("FileErrors","No error occurred."); - case QFile::ReadError: return QApplication::translate("FileErrors","An error occurred while reading from the file."); - case QFile::WriteError: return QApplication::translate("FileErrors","An error occurred while writing to the file."); - case QFile::FatalError: return QApplication::translate("FileErrors","A fatal error occurred."); - case QFile::ResourceError: return QApplication::translate("FileErrors","An resource error occurred."); - case QFile::OpenError: return QApplication::translate("FileErrors","The file could not be opened."); - case QFile::AbortError: return QApplication::translate("FileErrors","The operation was aborted."); - case QFile::TimeOutError: return QApplication::translate("FileErrors","A timeout occurred."); - case QFile::UnspecifiedError: return QApplication::translate("FileErrors","An unspecified error occurred."); - case QFile::RemoveError: return QApplication::translate("FileErrors","The file could not be removed."); - case QFile::RenameError: return QApplication::translate("FileErrors","The file could not be renamed."); - case QFile::PositionError: return QApplication::translate("FileErrors","The position in the file could not be changed."); - case QFile::ResizeError: return QApplication::translate("FileErrors","The file could not be resized."); - case QFile::PermissionsError: return QApplication::translate("FileErrors","The file could not be accessed."); - case QFile::CopyError: return QApplication::translate("FileErrors","The file could not be copied."); - } - return QString(); -} - -void openBrowser(IEntryHandle* entry){ - QString url = entry->url(); - url.replace("{TITLE}", entry->title(), Qt::CaseInsensitive); - url.replace("{USERNAME}", entry->username(), Qt::CaseInsensitive); - - if (url.contains("{PASSWORD}",Qt::CaseInsensitive)){ - SecString password=entry->password(); - password.unlock(); - url.replace("{PASSWORD}", password, Qt::CaseInsensitive); - } - - openBrowser(url); -} - -void openBrowser(const QString& UrlString){ - if (UrlString.startsWith("cmd://") && UrlString.length()>6){ - QProcess::startDetached(UrlString.right(UrlString.length()-6)); - return; - } - - QUrl url(UrlString); - if(url.scheme().isEmpty()) - url=QUrl("http://"+UrlString); - if(config->urlCmdDef() || url.scheme()=="mailto"){ - QDesktopServices::openUrl(url); - } - else{ - QByteArray UrlEncoded = url.toEncoded(); - QString browser = config->urlCmd(); - if (browser.contains("%u", Qt::CaseInsensitive)) - browser.replace("%u", UrlEncoded, Qt::CaseInsensitive); - else if (browser.contains("%1")) - browser.replace("%1", UrlEncoded); - else - browser.append(" ").append(UrlEncoded); - QProcess::startDetached(browser); - } -} - -QString getImageFile(const QString& name){ - if (QFile::exists(DataDir+"/icons/"+name)) - return DataDir+"/icons/"+name; - else{ - QMessageBox::critical(0,QApplication::translate("Main","Error"), - QApplication::translate("Main","File '%1' could not be found.") - .arg(name),QApplication::translate("Main","OK"),0,0,2,1); - exit(1); - } -} - ///TODO 0.2.3 remove void loadImages(){ QPixmap tmpImg(getImageFile("clientic.png")); @@ -347,39 +208,6 @@ void loadImages(){ } -const QIcon& getIcon(const QString& name){ - QIcon* CachedIcon=IconCache.value(name); - if(CachedIcon) - return *CachedIcon; - QIcon* NewIcon=NULL; - //TODO plugins - /*if(IconLoader){ - NewIcon=new QIcon(IconLoader->getIcon(name)); - if(NewIcon->isNull()){ - delete NewIcon; - NewIcon=NULL; - } - else - IconCache.insert(name,NewIcon); - }*/ - if(!NewIcon) - { - NewIcon=new QIcon(getImageFile(name+".png")); - IconCache.insert(name,NewIcon); - } - return *NewIcon; -} - -const QPixmap* getPixmap(const QString& name){ - QPixmap* CachedPixmap=PixmapCache.value(name); - if(CachedPixmap) - return CachedPixmap; - QImage img(getImageFile(name+".png")); - QPixmap* NewPixmap=new QPixmap(QPixmap::fromImage(img)); - PixmapCache.insert(name,NewPixmap); - return NewPixmap; -} - bool loadTranslation(QTranslator* tr,const QString& prefix,const QString& loc,const QStringList& paths){ for(int i=0;i1){ - int i=1; - if(argv[i][0]!='-'){ - ArgFile=QString::fromUtf8(argv[i]); - i++; - } - for(; i(chars), length); - - QVarLengthArray buffer(length); - CFStringGetCharacters(cfPath, CFRangeMake(0, length), buffer.data()); - return QString(reinterpret_cast(buffer.constData()), length); - - #elif defined( Q_OS_UNIX ) - #ifdef Q_OS_LINUX - // Try looking for a /proc//exe symlink first which points to - // the absolute path of the executable - QFileInfo pfi(QString::fromLatin1("/proc/%1/exe").arg(getpid())); - if (pfi.exists() && pfi.isSymLink()) - return pfi.canonicalFilePath(); - #endif - - QString argv0 = QFile::decodeName(QByteArray(argv[0])); - QString absPath; - - if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) { - /* - If argv0 starts with a slash, it is already an absolute - file path. - */ - absPath = argv0; - } else if (argv0.contains(QLatin1Char('/'))) { - /* - If argv0 contains one or more slashes, it is a file path - relative to the current directory. - */ - absPath = QDir::current().absoluteFilePath(argv0); - } else { - /* - Otherwise, the file path has to be determined using the - PATH environment variable. - */ - QByteArray pEnv = qgetenv("PATH"); - QDir currentDir = QDir::current(); - QStringList paths = QString::fromLocal8Bit(pEnv.constData()).split(QLatin1String(":")); - for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) { - if ((*p).isEmpty()) - continue; - QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0); - QFileInfo candidate_fi(candidate); - if (candidate_fi.exists() && !candidate_fi.isDir()) { - absPath = candidate; - break; - } - } - } - - absPath = QDir::cleanPath(absPath); - - QFileInfo fi(absPath); - return fi.exists() ? fi.canonicalFilePath() : QString(); - #endif -} + diff --git a/src/main.h b/src/main.h index 9dd43fc..7ff7b58 100644 --- a/src/main.h +++ b/src/main.h @@ -37,27 +37,11 @@ #define BUILTIN_ICONS 65 -#define CSTR(x)(x.toLocal8Bit().constData()) -class IEntryHandle; +//QString findPlugin(const QString& filename); //TODO Plugins -typedef enum tKeyType {PASSWORD=0,KEYFILE=1,BOTH=2}; class KpxConfig; - -void createBanner(QPixmap* Pixmap, const QPixmap* IconAlpha,const QString& Text,int Width); -void createBanner(QPixmap* Pixmap, const QPixmap* IconAlpha,const QString& Text,int Width, QColor Color1, QColor Color2, QColor TextColor); -void openBrowser(IEntryHandle* entry); -void openBrowser(const QString& UrlString); -void showErrMsg(const QString& msg,QWidget* parent=NULL); -QString applicationFilePath(); -QString applicationDirPath(); -const QIcon& getIcon(const QString& name); -const QPixmap* getPixmap(const QString& name); -QString decodeFileError(QFile::FileError Code); -//QString findPlugin(const QString& filename); //TODO Plugins -QString makePathRelative(const QString& Abs,const QString& Cur); extern QString PluginLoadError; - extern KpxConfig *config; extern QString AppDir; extern QString HomeDir; @@ -67,4 +51,26 @@ extern QString DetailViewTemplate; extern QPixmap *EntryIcons; +class CmdLineArgs { +public: + CmdLineArgs(); + bool parse(const QStringList& argv); + static void printHelp(); + QString error() {return Error;} + QString file() {return File;} + QString configLocation() {return ConfigLocation;} + QString language() {return Language;} + bool startMinimized() {return StartMinimized;} + bool startLocked() {return StartLocked;} + bool help() {return Help;} +private: + QString Error; + QString File; + QString ConfigLocation; + QString Language; + bool StartMinimized; + bool StartLocked; + bool Help; +}; + #endif diff --git a/src/main_macx.cpp b/src/main_macx.cpp new file mode 100644 index 0000000..73353b0 --- /dev/null +++ b/src/main_macx.cpp @@ -0,0 +1,43 @@ +/*************************************************************************** + * Copyright (C) 1992-2008 Trolltech ASA * + * * + * Copyright (C) 2005-2008 by Tarek Saidi * + * tarek.saidi@arcor.de * + * * + * 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; version 2 of the License. * + * * + * 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., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include +#include "main.h" + +void initAppPaths() { + CFURLRef bundleURL(CFBundleCopyExecutableURL(CFBundleGetMainBundle())); + //assert(bundleURL); + CFStringRef cfPath(CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle)); + //assert(cfPath); + CFIndex length = CFStringGetLength(cfPath); + const UniChar *chars = CFStringGetCharactersPtr(cfPath); + if (chars) { + AppDir = QString(reinterpret_cast(chars), length); + } + else { + QVarLengthArray buffer(length); + CFStringGetCharacters(cfPath, CFRangeMake(0, length), buffer.data()); + AppDir = QString(reinterpret_cast(buffer.constData()), length); + } + AppDir.truncate(AppDir.lastIndexOf("/")); + HomeDir = QDir::homePath()+"/.keepassx"; + DataDir=AppDir+"/../Resources/keepassx"; +} diff --git a/src/main_unix.cpp b/src/main_unix.cpp new file mode 100644 index 0000000..34e5507 --- /dev/null +++ b/src/main_unix.cpp @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 1992-2008 Trolltech ASA * + * * + * Copyright (C) 2005-2008 by Tarek Saidi * + * tarek.saidi@arcor.de * + * * + * 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; version 2 of the License. * + * * + * 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., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include +#include "main.h" + +void initAppPaths(int argc,char** argv) { + // Try looking for a /proc//exe symlink first which points to + // the absolute path of the executable + QFileInfo pfi(QString::fromLatin1("/proc/%1/exe").arg(getpid())); + if (pfi.exists() && pfi.isSymLink()) { + AppDir = pfi.canonicalFilePath(); + } + else { + QString argv0 = QFile::decodeName(QByteArray(argv[0])); + QString absPath; + + if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) { + /* + If argv0 starts with a slash, it is already an absolute + file path. + */ + absPath = argv0; + } else if (argv0.contains(QLatin1Char('/'))) { + /* + If argv0 contains one or more slashes, it is a file path + relative to the current directory. + */ + absPath = QDir::current().absoluteFilePath(argv0); + } else { + /* + Otherwise, the file path has to be determined using the + PATH environment variable. + */ + QByteArray pEnv = qgetenv("PATH"); + QDir currentDir = QDir::current(); + QStringList paths = QString::fromLocal8Bit(pEnv.constData()).split(QLatin1String(":")); + for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) { + if ((*p).isEmpty()) + continue; + QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0); + QFileInfo candidate_fi(candidate); + if (candidate_fi.exists() && !candidate_fi.isDir()) { + absPath = candidate; + break; + } + } + } + absPath = QDir::cleanPath(absPath); + QFileInfo fi(absPath); + AppDir = fi.exists() ? fi.canonicalFilePath() : QString(); + } + AppDir.truncate(AppDir.lastIndexOf("/")); + DataDir=AppDir+"/../share/keepassx"; + HomeDir = QDir::homePath()+"/.keepassx"; +} \ No newline at end of file diff --git a/src/main_win32.cpp b/src/main_win32.cpp new file mode 100644 index 0000000..e567425 --- /dev/null +++ b/src/main_win32.cpp @@ -0,0 +1,46 @@ +/*************************************************************************** + * Copyright (C) 1992-2007 Trolltech ASA * + * * + * Copyright (C) 2005-2007 by Tarek Saidi * + * tarek.saidi@arcor.de * + * * + * 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; version 2 of the License. * + * * + * 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., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include +#include "main.h" + +void initAppPaths(int argc,char** argv){ + QFileInfo filePath; + QT_WA({ + wchar_t module_name[256]; + GetModuleFileNameW(0, module_name, sizeof(module_name) / sizeof(wchar_t)); + filePath = QString::fromUtf16((ushort *)module_name); + }, { + char module_name[256]; + GetModuleFileNameA(0, module_name, sizeof(module_name)); + filePath = QString::fromLocal8Bit(module_name); + }); + AppDir = filePath.filePath(); + AppDir.truncate(AppDir.lastIndexOf("/")); + + HomeDir = QString::fromLocal8Bit(qgetenv("APPDATA").constData()); + if(!HomeDir.isEmpty() && QFile::exists(HomeDir)) + HomeDir = QDir::fromNativeSeparators(HomeDir)+"/KeePassX"; + else + HomeDir = QDir::homePath()+"/KeePassX"; + + DataDir=AppDir+"/share"; +} \ No newline at end of file diff --git a/src/src.pro b/src/src.pro index e081d32..e28c49c 100644 --- a/src/src.pro +++ b/src/src.pro @@ -48,6 +48,7 @@ unix : !macx : !isEqual(QMAKE_WIN32,1) { SOURCES += Application_X11.cpp HEADERS += Application_X11.h } + SOURCES += main_unix.cpp } @@ -78,6 +79,7 @@ macx { isEqual(ARCH,UNIVERSAL) : CONFIG += x86 ppc isEqual(ARCH,INTEL) : CONFIG += x86 isEqual(ARCH,PPC) : CONFIG += ppc + SOURCES += main_macx.cpp } #------------------------------------------------------------------------------- @@ -97,6 +99,7 @@ isEqual(QMAKE_WIN32,1) { } RC_FILE = ../share/win_ico/keepassx.rc QMAKE_LINK_OBJECT_SCRIPT = $${OBJECTS_DIR}/$${QMAKE_LINK_OBJECT_SCRIPT} + SOURCES += main_win32.cpp } @@ -173,6 +176,7 @@ HEADERS += lib/UrlLabel.h \ lib/ShortcutWidget.h \ global.h \ main.h \ + lib/tools.h \ lib/GroupView.h \ lib/EntryView.h \ crypto/arcfour.h \ @@ -230,6 +234,7 @@ SOURCES += lib/UrlLabel.cpp \ # dialogs/TrashCanDlg.cpp \ lib/random.cpp \ Database.cpp \ + lib/tools.cpp \ # lib/KdePlugin.cpp \ lib/GroupView.cpp \ lib/EntryView.cpp \