2010-08-07 09:10:44 -04:00
/*
* Copyright ( C ) 2010 Felix Geyer < debfx @ fobos . 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 , either version 2 or ( at your option )
* version 3 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 , see < http : //www.gnu.org/licenses/>.
*/
2015-07-22 11:02:50 -04:00
# include <QCommandLineParser>
2013-10-03 09:18:16 -04:00
# include <QFile>
2016-10-24 14:22:02 -04:00
# include <QTextStream>
2010-08-15 06:31:48 -04:00
2014-01-18 09:31:24 -05:00
# include "config-keepassx.h"
2013-03-29 15:35:54 -04:00
# include "core/Config.h"
2012-10-13 05:05:50 -04:00
# include "core/Tools.h"
2014-05-17 19:33:22 -04:00
# include "core/Translator.h"
2010-09-13 17:25:48 -04:00
# include "crypto/Crypto.h"
2012-05-17 18:35:24 -04:00
# include "gui/Application.h"
2010-09-19 10:59:32 -04:00
# include "gui/MainWindow.h"
2014-06-15 05:17:40 -04:00
# include "gui/MessageBox.h"
2010-08-15 06:31:48 -04:00
2017-01-25 19:15:12 -05:00
# ifdef QT_STATIC
# include <QtPlugin>
# ifdef Q_OS_WIN
Q_IMPORT_PLUGIN ( QWindowsIntegrationPlugin )
# elif Q_OS_LINUX
Q_IMPORT_PLUGIN ( QXcbIntegrationPlugin )
# endif
# endif
2012-05-02 11:04:03 -04:00
int main ( int argc , char * * argv )
2010-08-07 09:10:44 -04:00
{
2012-10-13 05:05:50 -04:00
# ifdef QT_NO_DEBUG
Tools : : disableCoreDumps ( ) ;
# endif
2015-12-06 08:32:06 -05:00
Tools : : setupSearchPaths ( ) ;
2017-02-19 17:42:05 -05:00
2012-05-17 18:35:24 -04:00
Application app ( argc , argv ) ;
2016-11-03 00:05:30 -04:00
Application : : setApplicationName ( " keepassxc " ) ;
2014-01-18 09:31:24 -05:00
Application : : setApplicationVersion ( KEEPASSX_VERSION ) ;
// don't set organizationName as that changes the return value of
2015-09-12 12:41:48 -04:00
// QStandardPaths::writableLocation(QDesktopServices::DataLocation)
2010-08-15 06:31:48 -04:00
2014-11-02 04:15:44 -05:00
QApplication : : setQuitOnLastWindowClosed ( false ) ;
2014-06-15 05:17:40 -04:00
if ( ! Crypto : : init ( ) ) {
QString error = QCoreApplication : : translate ( " Main " ,
" Fatal error while testing the cryptographic functions. " ) ;
error . append ( " \n " ) ;
error . append ( Crypto : : errorString ( ) ) ;
2016-11-03 00:05:30 -04:00
MessageBox : : critical ( nullptr , QCoreApplication : : translate ( " Main " , " KeePassXC - Error " ) , error ) ;
2014-06-15 05:17:40 -04:00
return 1 ;
}
2010-09-13 17:25:48 -04:00
2014-01-18 09:23:55 -05:00
QCommandLineParser parser ;
2016-11-03 00:05:30 -04:00
parser . setApplicationDescription ( QCoreApplication : : translate ( " main " , " KeePassXC - cross-platform password manager " ) ) ;
2015-06-23 05:40:54 -04:00
parser . addPositionalArgument ( " filename " , QCoreApplication : : translate ( " main " , " filename(s) of the password database(s) to open (*.kdbx) " ) , " [filename(s)] " ) ;
2013-03-29 15:35:54 -04:00
2014-01-18 09:23:55 -05:00
QCommandLineOption configOption ( " config " ,
QCoreApplication : : translate ( " main " , " path to a custom config file " ) ,
" config " ) ;
QCommandLineOption keyfileOption ( " keyfile " ,
QCoreApplication : : translate ( " main " , " key file of the database " ) ,
" keyfile " ) ;
2016-10-24 14:22:02 -04:00
QCommandLineOption pwstdinOption ( " pw-stdin " ,
QCoreApplication : : translate ( " main " , " read password of the database from stdin " ) ) ;
2014-01-18 09:23:55 -05:00
parser . addHelpOption ( ) ;
2014-01-18 09:31:24 -05:00
parser . addVersionOption ( ) ;
2014-01-18 09:23:55 -05:00
parser . addOption ( configOption ) ;
parser . addOption ( keyfileOption ) ;
2016-10-24 14:22:02 -04:00
parser . addOption ( pwstdinOption ) ;
2014-01-18 09:23:55 -05:00
parser . process ( app ) ;
const QStringList args = parser . positionalArguments ( ) ;
if ( parser . isSet ( configOption ) ) {
Config : : createConfigFromFile ( parser . value ( configOption ) ) ;
2012-04-24 05:47:16 -04:00
}
2014-05-17 19:33:22 -04:00
Translator : : installTranslator ( ) ;
2013-03-30 06:16:24 -04:00
# ifdef Q_OS_MAC
// Don't show menu icons on OSX
QApplication : : setAttribute ( Qt : : AA_DontShowIconsInMenus ) ;
# endif
2010-09-19 10:59:32 -04:00
MainWindow mainWindow ;
2015-10-10 11:00:50 -04:00
app . setMainWindow ( & mainWindow ) ;
2016-11-23 18:15:51 -05:00
2012-05-21 16:11:26 -04:00
QObject : : connect ( & app , SIGNAL ( openFile ( QString ) ) , & mainWindow , SLOT ( openDatabase ( QString ) ) ) ;
2016-11-23 18:15:51 -05:00
2016-11-26 09:37:25 -05:00
// start minimized if configured
2017-02-14 13:08:14 -05:00
bool minimizeOnStartup = config ( ) - > get ( " GUI/MinimizeOnStartup " ) . toBool ( ) ;
bool minimizeToTray = config ( ) - > get ( " GUI/MinimizeToTray " ) . toBool ( ) ;
if ( minimizeOnStartup ) {
2016-11-26 09:37:25 -05:00
mainWindow . setWindowState ( Qt : : WindowMinimized ) ;
2017-02-14 13:08:14 -05:00
}
if ( ! ( minimizeOnStartup & & minimizeToTray ) ) {
2017-02-14 09:55:33 -05:00
mainWindow . show ( ) ;
2016-11-26 09:37:25 -05:00
}
2015-06-23 05:40:54 -04:00
for ( int ii = 0 ; ii < args . length ( ) ; ii + + ) {
QString filename = args [ ii ] ;
2014-01-18 09:23:55 -05:00
if ( ! filename . isEmpty ( ) & & QFile : : exists ( filename ) ) {
2016-10-24 14:22:02 -04:00
QString password ;
if ( parser . isSet ( pwstdinOption ) ) {
static QTextStream in ( stdin , QIODevice : : ReadOnly ) ;
password = in . readLine ( ) ;
}
mainWindow . openDatabase ( filename , password , parser . value ( keyfileOption ) ) ;
2014-01-18 09:23:55 -05:00
}
2012-04-24 05:47:16 -04:00
}
2013-04-18 16:15:01 -04:00
if ( config ( ) - > get ( " OpenPreviousDatabasesOnStartup " ) . toBool ( ) ) {
2016-09-02 13:51:51 -04:00
const QStringList filenames = config ( ) - > get ( " LastOpenedDatabases " ) . toStringList ( ) ;
for ( const QString & filename : filenames ) {
2013-04-18 16:15:01 -04:00
if ( ! filename . isEmpty ( ) & & QFile : : exists ( filename ) ) {
2013-04-18 16:15:01 -04:00
mainWindow . openDatabase ( filename , QString ( ) , QString ( ) ) ;
2013-04-18 16:15:01 -04:00
}
}
}
2016-11-23 18:15:51 -05:00
2010-08-15 06:31:48 -04:00
return app . exec ( ) ;
2010-08-07 09:10:44 -04:00
}