1bb02e0acSNicolas Bonnefon /* 2812146a8SNicolas Bonnefon * Copyright (C) 2009, 2010, 2011, 2013, 2014 Nicolas Bonnefon and other contributors 3bb02e0acSNicolas Bonnefon * 4bb02e0acSNicolas Bonnefon * This file is part of glogg. 5bb02e0acSNicolas Bonnefon * 6bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9bb02e0acSNicolas Bonnefon * (at your option) any later version. 10bb02e0acSNicolas Bonnefon * 11bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15bb02e0acSNicolas Bonnefon * 16bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18bb02e0acSNicolas Bonnefon */ 19bb02e0acSNicolas Bonnefon 20bb02e0acSNicolas Bonnefon #include <QApplication> 214752846bSNicolas Bonnefon #include <QFileInfo> 22bb02e0acSNicolas Bonnefon 23d96f3f21SNicolas Bonnefon #include <memory> 24d96f3f21SNicolas Bonnefon 25bb02e0acSNicolas Bonnefon #include <boost/program_options.hpp> 26bb02e0acSNicolas Bonnefon namespace po = boost::program_options; 27bb02e0acSNicolas Bonnefon 28bb02e0acSNicolas Bonnefon #include <iostream> 29e07da50aSNicolas Bonnefon #include <iomanip> 30bb02e0acSNicolas Bonnefon using namespace std; 31bb02e0acSNicolas Bonnefon 32320f9110SNicolas Bonnefon #ifdef _WIN32 33320f9110SNicolas Bonnefon #include "unistd.h" 34320f9110SNicolas Bonnefon #endif 35320f9110SNicolas Bonnefon 36bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 37bb02e0acSNicolas Bonnefon #include "sessioninfo.h" 38bb02e0acSNicolas Bonnefon #include "configuration.h" 39bb02e0acSNicolas Bonnefon #include "filterset.h" 40bb02e0acSNicolas Bonnefon #include "recentfiles.h" 41d96f3f21SNicolas Bonnefon #include "session.h" 42bb02e0acSNicolas Bonnefon #include "mainwindow.h" 43bb02e0acSNicolas Bonnefon #include "savedsearches.h" 44812146a8SNicolas Bonnefon #include "loadingstatus.h" 4515af0893SNicolas Bonnefon 4615af0893SNicolas Bonnefon #include "externalcom.h" 47*0374f408SAnton Filimonov 4815af0893SNicolas Bonnefon #ifdef GLOGG_SUPPORTS_DBUS 49557fb9d8SNicolas Bonnefon #include "dbusexternalcom.h" 5024895119SNicolas Bonnefon #elif GLOGG_SUPPORTS_WINIPC 51*0374f408SAnton Filimonov #include "socketexternalcom.h" 5215af0893SNicolas Bonnefon #endif 5315af0893SNicolas Bonnefon 5424895119SNicolas Bonnefon 55bb02e0acSNicolas Bonnefon #include "log.h" 56bb02e0acSNicolas Bonnefon 57bb02e0acSNicolas Bonnefon static void print_version(); 58bb02e0acSNicolas Bonnefon 59bb02e0acSNicolas Bonnefon int main(int argc, char *argv[]) 60bb02e0acSNicolas Bonnefon { 61bb02e0acSNicolas Bonnefon QApplication app(argc, argv); 62bb02e0acSNicolas Bonnefon 63bb02e0acSNicolas Bonnefon string filename = ""; 64bb02e0acSNicolas Bonnefon 65da4beab3SNicolas Bonnefon // Configuration 66da4beab3SNicolas Bonnefon bool new_session = false; 671e522573SNicolas Bonnefon bool load_session = false; 68da4beab3SNicolas Bonnefon bool multi_instance = false; 69320f9110SNicolas Bonnefon #ifdef _WIN32 70320f9110SNicolas Bonnefon bool log_to_file = false; 71320f9110SNicolas Bonnefon #endif 72da4beab3SNicolas Bonnefon 73bb02e0acSNicolas Bonnefon TLogLevel logLevel = logWARNING; 74bb02e0acSNicolas Bonnefon 75bb02e0acSNicolas Bonnefon try { 76bb02e0acSNicolas Bonnefon po::options_description desc("Usage: glogg [options] [file]"); 77bb02e0acSNicolas Bonnefon desc.add_options() 78bb02e0acSNicolas Bonnefon ("help,h", "print out program usage (this message)") 79bb02e0acSNicolas Bonnefon ("version,v", "print glogg's version information") 80da4beab3SNicolas Bonnefon ("multi,m", "allow multiple instance of glogg to run simultaneously (use together with -s)") 811e522573SNicolas Bonnefon ("load-session,s", "load the previous session (default when no file is passed)") 821e522573SNicolas Bonnefon ("new-session,n", "do not load the previous session (default when a file is passed)") 83320f9110SNicolas Bonnefon #ifdef _WIN32 84320f9110SNicolas Bonnefon ("log,l", "save the log to a file (Windows only)") 85320f9110SNicolas Bonnefon #endif 861e522573SNicolas Bonnefon ("debug,d", "output more debug (include multiple times for more verbosity e.g. -dddd)") 87bb02e0acSNicolas Bonnefon ; 88bb02e0acSNicolas Bonnefon po::options_description desc_hidden("Hidden options"); 89bb02e0acSNicolas Bonnefon // For -dd, -ddd... 90bb02e0acSNicolas Bonnefon for ( string s = "dd"; s.length() <= 10; s.append("d") ) 91bb02e0acSNicolas Bonnefon desc_hidden.add_options()(s.c_str(), "debug"); 92bb02e0acSNicolas Bonnefon 93bb02e0acSNicolas Bonnefon desc_hidden.add_options() 94bb02e0acSNicolas Bonnefon ("input-file", po::value<string>(), "input file") 95bb02e0acSNicolas Bonnefon ; 96bb02e0acSNicolas Bonnefon 97bb02e0acSNicolas Bonnefon po::options_description all_options("all options"); 98bb02e0acSNicolas Bonnefon all_options.add(desc).add(desc_hidden); 99bb02e0acSNicolas Bonnefon 100bb02e0acSNicolas Bonnefon po::positional_options_description positional; 101bb02e0acSNicolas Bonnefon positional.add("input-file", 1); 102bb02e0acSNicolas Bonnefon 103bb02e0acSNicolas Bonnefon int command_line_style = (((po::command_line_style::unix_style ^ 104bb02e0acSNicolas Bonnefon po::command_line_style::allow_guessing) | 105bb02e0acSNicolas Bonnefon po::command_line_style::allow_long_disguise) ^ 106bb02e0acSNicolas Bonnefon po::command_line_style::allow_sticky); 107bb02e0acSNicolas Bonnefon 108bb02e0acSNicolas Bonnefon po::variables_map vm; 109bb02e0acSNicolas Bonnefon po::store(po::command_line_parser(argc, argv). 110bb02e0acSNicolas Bonnefon options(all_options). 111bb02e0acSNicolas Bonnefon positional(positional). 112bb02e0acSNicolas Bonnefon style(command_line_style).run(), 113bb02e0acSNicolas Bonnefon vm); 114bb02e0acSNicolas Bonnefon po::notify(vm); 115bb02e0acSNicolas Bonnefon 116bb02e0acSNicolas Bonnefon if ( vm.count("help") ) { 117bb02e0acSNicolas Bonnefon desc.print(cout); 118bb02e0acSNicolas Bonnefon return 0; 119bb02e0acSNicolas Bonnefon } 120bb02e0acSNicolas Bonnefon 121bb02e0acSNicolas Bonnefon if ( vm.count("version") ) { 122bb02e0acSNicolas Bonnefon print_version(); 123bb02e0acSNicolas Bonnefon return 0; 124bb02e0acSNicolas Bonnefon } 125bb02e0acSNicolas Bonnefon 126da4beab3SNicolas Bonnefon if ( vm.count( "debug" ) ) 127bb02e0acSNicolas Bonnefon logLevel = logINFO; 128da4beab3SNicolas Bonnefon 129da4beab3SNicolas Bonnefon if ( vm.count( "multi" ) ) 130da4beab3SNicolas Bonnefon multi_instance = true; 131da4beab3SNicolas Bonnefon 132da4beab3SNicolas Bonnefon if ( vm.count( "new-session" ) ) 133da4beab3SNicolas Bonnefon new_session = true; 134bb02e0acSNicolas Bonnefon 1351e522573SNicolas Bonnefon if ( vm.count( "load-session" ) ) 1361e522573SNicolas Bonnefon load_session = true; 1371e522573SNicolas Bonnefon 138320f9110SNicolas Bonnefon #ifdef _WIN32 139320f9110SNicolas Bonnefon if ( vm.count( "log" ) ) 140320f9110SNicolas Bonnefon log_to_file = true; 141320f9110SNicolas Bonnefon #endif 142320f9110SNicolas Bonnefon 143bb02e0acSNicolas Bonnefon for ( string s = "dd"; s.length() <= 10; s.append("d") ) 144bb02e0acSNicolas Bonnefon if ( vm.count( s ) ) 145bb02e0acSNicolas Bonnefon logLevel = (TLogLevel) (logWARNING + s.length()); 146bb02e0acSNicolas Bonnefon 147661d48b0SNicolas Bonnefon if ( vm.count("input-file") ) { 148bb02e0acSNicolas Bonnefon filename = vm["input-file"].as<string>(); 149bb02e0acSNicolas Bonnefon } 150661d48b0SNicolas Bonnefon } 151bb02e0acSNicolas Bonnefon catch(exception& e) { 152bb02e0acSNicolas Bonnefon cerr << "Option processing error: " << e.what() << endl; 153bb02e0acSNicolas Bonnefon return 1; 154bb02e0acSNicolas Bonnefon } 155bb02e0acSNicolas Bonnefon catch(...) { 156bb02e0acSNicolas Bonnefon cerr << "Exception of unknown type!\n"; 157bb02e0acSNicolas Bonnefon } 158bb02e0acSNicolas Bonnefon 159320f9110SNicolas Bonnefon #ifdef _WIN32 160320f9110SNicolas Bonnefon if ( log_to_file ) 161320f9110SNicolas Bonnefon { 162320f9110SNicolas Bonnefon char file_name[255]; 163320f9110SNicolas Bonnefon snprintf( file_name, sizeof file_name, "glogg_%d.log", getpid() ); 164320f9110SNicolas Bonnefon FILE* file = fopen(file_name, "w"); 165bb02e0acSNicolas Bonnefon Output2FILE::Stream() = file; 166320f9110SNicolas Bonnefon } 167bb02e0acSNicolas Bonnefon #endif 168bb02e0acSNicolas Bonnefon 169bb02e0acSNicolas Bonnefon FILELog::setReportingLevel( logLevel ); 170bb02e0acSNicolas Bonnefon 17165076e27SNicolas Bonnefon if ( ! filename.empty() ) { 17265076e27SNicolas Bonnefon // Convert to absolute path 173661d48b0SNicolas Bonnefon QFileInfo file( QString::fromLocal8Bit( filename.c_str() ) ); 17465076e27SNicolas Bonnefon filename = file.absoluteFilePath().toStdString(); 175661d48b0SNicolas Bonnefon LOG( logDEBUG ) << "Filename: " << filename; 17665076e27SNicolas Bonnefon } 17765076e27SNicolas Bonnefon 178e07da50aSNicolas Bonnefon // External communicator 179da4beab3SNicolas Bonnefon shared_ptr<ExternalCommunicator> externalCommunicator = nullptr; 180da4beab3SNicolas Bonnefon shared_ptr<ExternalInstance> externalInstance = nullptr; 181da4beab3SNicolas Bonnefon 182da4beab3SNicolas Bonnefon try { 18315af0893SNicolas Bonnefon #ifdef GLOGG_SUPPORTS_DBUS 184da4beab3SNicolas Bonnefon externalCommunicator = make_shared<DBusExternalCommunicator>(); 18524895119SNicolas Bonnefon #elif GLOGG_SUPPORTS_WINIPC 186*0374f408SAnton Filimonov externalCommunicator = make_shared<SocketExternalCommunicator>(); 187*0374f408SAnton Filimonov #endif 18824895119SNicolas Bonnefon externalInstance = shared_ptr<ExternalInstance>( 18924895119SNicolas Bonnefon externalCommunicator->otherInstance() ); 190da4beab3SNicolas Bonnefon } 191da4beab3SNicolas Bonnefon catch(CantCreateExternalErr& e) { 192da4beab3SNicolas Bonnefon LOG(logWARNING) << "Cannot initialise external communication."; 193da4beab3SNicolas Bonnefon } 194557fb9d8SNicolas Bonnefon 195e07da50aSNicolas Bonnefon LOG(logDEBUG) << "externalInstance = " << externalInstance; 196da4beab3SNicolas Bonnefon if ( ( ! multi_instance ) && externalInstance ) { 197e07da50aSNicolas Bonnefon uint32_t version = externalInstance->getVersion(); 198e07da50aSNicolas Bonnefon LOG(logINFO) << "Found another glogg (version = " 199e07da50aSNicolas Bonnefon << std::setbase(16) << version << ")"; 200e07da50aSNicolas Bonnefon 20109aff35dSNicolas Bonnefon externalInstance->loadFile( QString::fromStdString( filename ) ); 20209aff35dSNicolas Bonnefon 203e07da50aSNicolas Bonnefon return 0; 204e07da50aSNicolas Bonnefon } 205e07da50aSNicolas Bonnefon else { 206e07da50aSNicolas Bonnefon // FIXME: there is a race condition here. One glogg could start 207e07da50aSNicolas Bonnefon // between the declaration of externalInstance and here, 208e07da50aSNicolas Bonnefon // is it a problem? 209da4beab3SNicolas Bonnefon if ( externalCommunicator ) 210e07da50aSNicolas Bonnefon externalCommunicator->startListening(); 211e07da50aSNicolas Bonnefon } 212e07da50aSNicolas Bonnefon 213812146a8SNicolas Bonnefon // Register types for Qt 214812146a8SNicolas Bonnefon qRegisterMetaType<LoadingStatus>("LoadingStatus"); 215812146a8SNicolas Bonnefon 216bb02e0acSNicolas Bonnefon // Register the configuration items 217bb02e0acSNicolas Bonnefon GetPersistentInfo().migrateAndInit(); 218bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 21911582726SNicolas Bonnefon std::make_shared<SessionInfo>(), QString( "session" ) ); 220bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 22111582726SNicolas Bonnefon std::make_shared<Configuration>(), QString( "settings" ) ); 222bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 22311582726SNicolas Bonnefon std::make_shared<FilterSet>(), QString( "filterSet" ) ); 224bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 22511582726SNicolas Bonnefon std::make_shared<SavedSearches>(), QString( "savedSearches" ) ); 226bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 22711582726SNicolas Bonnefon std::make_shared<RecentFiles>(), QString( "recentFiles" ) ); 228a3f81147SNicolas Bonnefon #ifdef GLOGG_SUPPORTS_VERSION_CHECKING 229a3f81147SNicolas Bonnefon GetPersistentInfo().registerPersistable( 230a3f81147SNicolas Bonnefon std::make_shared<VersionCheckerConfig>(), QString( "versionChecker" ) ); 231a3f81147SNicolas Bonnefon #endif 232bb02e0acSNicolas Bonnefon 233cf609627SNicolas Bonnefon #ifdef _WIN32 234cf609627SNicolas Bonnefon // Allow the app to raise it's own windows (in case an external 235cf609627SNicolas Bonnefon // glogg send us a file to open) 236cf609627SNicolas Bonnefon AllowSetForegroundWindow(ASFW_ANY); 237cf609627SNicolas Bonnefon #endif 238cf609627SNicolas Bonnefon 2397999f43eSNicolas Bonnefon // We support high-dpi (aka Retina) displays 2407999f43eSNicolas Bonnefon app.setAttribute( Qt::AA_UseHighDpiPixmaps ); 241a3dab889SNicolas Bonnefon 242a3dab889SNicolas Bonnefon // No icon in menus 243a3dab889SNicolas Bonnefon app.setAttribute( Qt::AA_DontShowIconsInMenus ); 2447999f43eSNicolas Bonnefon 245bb02e0acSNicolas Bonnefon // FIXME: should be replaced by a two staged init of MainWindow 246bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "settings" ) ); 247bb02e0acSNicolas Bonnefon 248d96f3f21SNicolas Bonnefon std::unique_ptr<Session> session( new Session() ); 24909aff35dSNicolas Bonnefon MainWindow mw( std::move( session ), externalCommunicator ); 250bb02e0acSNicolas Bonnefon 251bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "MainWindow created."; 2520f9fd9edSNicolas Bonnefon mw.show(); 2533b104697SAnton Filimonov 2541e522573SNicolas Bonnefon // Load the existing session if needed 2553b104697SAnton Filimonov std::shared_ptr<Configuration> config = 2563b104697SAnton Filimonov Persistent<Configuration>( "settings" ); 2573b104697SAnton Filimonov if ( load_session || ( filename.empty() && !new_session && config->loadLastSession() ) ) 2580f9fd9edSNicolas Bonnefon mw.reloadSession(); 2593b104697SAnton Filimonov 2600f9fd9edSNicolas Bonnefon mw.loadInitialFile( QString::fromStdString( filename ) ); 2618a9275b2SNicolas Bonnefon mw.startBackgroundTasks(); 262a9448ba0SNicolas Bonnefon 263bb02e0acSNicolas Bonnefon return app.exec(); 264bb02e0acSNicolas Bonnefon } 265bb02e0acSNicolas Bonnefon 266bb02e0acSNicolas Bonnefon static void print_version() 267bb02e0acSNicolas Bonnefon { 268bb02e0acSNicolas Bonnefon cout << "glogg " GLOGG_VERSION "\n"; 269bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 270bb02e0acSNicolas Bonnefon cout << "Built " GLOGG_DATE " from " GLOGG_COMMIT "\n"; 271bb02e0acSNicolas Bonnefon #endif 272d022340fSNicolas Bonnefon cout << "Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicolas Bonnefon and other contributors\n"; 273bb02e0acSNicolas Bonnefon cout << "This is free software. You may redistribute copies of it under the terms of\n"; 274bb02e0acSNicolas Bonnefon cout << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"; 275bb02e0acSNicolas Bonnefon cout << "There is NO WARRANTY, to the extent permitted by law.\n"; 276bb02e0acSNicolas Bonnefon } 277