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> 21bb02e0acSNicolas Bonnefon 22d96f3f21SNicolas Bonnefon #include <memory> 23d96f3f21SNicolas Bonnefon 24bb02e0acSNicolas Bonnefon #include <boost/program_options.hpp> 25bb02e0acSNicolas Bonnefon namespace po = boost::program_options; 26bb02e0acSNicolas Bonnefon 27bb02e0acSNicolas Bonnefon #include <iostream> 28e07da50aSNicolas Bonnefon #include <iomanip> 29bb02e0acSNicolas Bonnefon using namespace std; 30bb02e0acSNicolas Bonnefon 31320f9110SNicolas Bonnefon #ifdef _WIN32 32320f9110SNicolas Bonnefon #include "unistd.h" 33320f9110SNicolas Bonnefon #endif 34320f9110SNicolas Bonnefon 35bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 36bb02e0acSNicolas Bonnefon #include "sessioninfo.h" 37bb02e0acSNicolas Bonnefon #include "configuration.h" 38bb02e0acSNicolas Bonnefon #include "filterset.h" 39bb02e0acSNicolas Bonnefon #include "recentfiles.h" 40d96f3f21SNicolas Bonnefon #include "session.h" 41bb02e0acSNicolas Bonnefon #include "mainwindow.h" 42bb02e0acSNicolas Bonnefon #include "savedsearches.h" 43812146a8SNicolas Bonnefon #include "loadingstatus.h" 4415af0893SNicolas Bonnefon 4515af0893SNicolas Bonnefon #include "externalcom.h" 4615af0893SNicolas Bonnefon #ifdef GLOGG_SUPPORTS_DBUS 47557fb9d8SNicolas Bonnefon #include "dbusexternalcom.h" 4824895119SNicolas Bonnefon #elif GLOGG_SUPPORTS_WINIPC 4924895119SNicolas Bonnefon #include "winexternalcom.h" 5015af0893SNicolas Bonnefon #endif 5115af0893SNicolas Bonnefon 5224895119SNicolas Bonnefon 53bb02e0acSNicolas Bonnefon #include "log.h" 54bb02e0acSNicolas Bonnefon 55bb02e0acSNicolas Bonnefon static void print_version(); 56bb02e0acSNicolas Bonnefon 57bb02e0acSNicolas Bonnefon int main(int argc, char *argv[]) 58bb02e0acSNicolas Bonnefon { 59bb02e0acSNicolas Bonnefon QApplication app(argc, argv); 60bb02e0acSNicolas Bonnefon 61bb02e0acSNicolas Bonnefon string filename = ""; 62bb02e0acSNicolas Bonnefon 63da4beab3SNicolas Bonnefon // Configuration 64da4beab3SNicolas Bonnefon bool new_session = false; 65*1e522573SNicolas Bonnefon bool load_session = false; 66da4beab3SNicolas Bonnefon bool multi_instance = false; 67320f9110SNicolas Bonnefon #ifdef _WIN32 68320f9110SNicolas Bonnefon bool log_to_file = false; 69320f9110SNicolas Bonnefon #endif 70da4beab3SNicolas Bonnefon 71bb02e0acSNicolas Bonnefon TLogLevel logLevel = logWARNING; 72bb02e0acSNicolas Bonnefon 73bb02e0acSNicolas Bonnefon try { 74bb02e0acSNicolas Bonnefon po::options_description desc("Usage: glogg [options] [file]"); 75bb02e0acSNicolas Bonnefon desc.add_options() 76bb02e0acSNicolas Bonnefon ("help,h", "print out program usage (this message)") 77bb02e0acSNicolas Bonnefon ("version,v", "print glogg's version information") 78da4beab3SNicolas Bonnefon ("multi,m", "allow multiple instance of glogg to run simultaneously (use together with -s)") 79*1e522573SNicolas Bonnefon ("load-session,s", "load the previous session (default when no file is passed)") 80*1e522573SNicolas Bonnefon ("new-session,n", "do not load the previous session (default when a file is passed)") 81320f9110SNicolas Bonnefon #ifdef _WIN32 82320f9110SNicolas Bonnefon ("log,l", "save the log to a file (Windows only)") 83320f9110SNicolas Bonnefon #endif 84*1e522573SNicolas Bonnefon ("debug,d", "output more debug (include multiple times for more verbosity e.g. -dddd)") 85bb02e0acSNicolas Bonnefon ; 86bb02e0acSNicolas Bonnefon po::options_description desc_hidden("Hidden options"); 87bb02e0acSNicolas Bonnefon // For -dd, -ddd... 88bb02e0acSNicolas Bonnefon for ( string s = "dd"; s.length() <= 10; s.append("d") ) 89bb02e0acSNicolas Bonnefon desc_hidden.add_options()(s.c_str(), "debug"); 90bb02e0acSNicolas Bonnefon 91bb02e0acSNicolas Bonnefon desc_hidden.add_options() 92bb02e0acSNicolas Bonnefon ("input-file", po::value<string>(), "input file") 93bb02e0acSNicolas Bonnefon ; 94bb02e0acSNicolas Bonnefon 95bb02e0acSNicolas Bonnefon po::options_description all_options("all options"); 96bb02e0acSNicolas Bonnefon all_options.add(desc).add(desc_hidden); 97bb02e0acSNicolas Bonnefon 98bb02e0acSNicolas Bonnefon po::positional_options_description positional; 99bb02e0acSNicolas Bonnefon positional.add("input-file", 1); 100bb02e0acSNicolas Bonnefon 101bb02e0acSNicolas Bonnefon int command_line_style = (((po::command_line_style::unix_style ^ 102bb02e0acSNicolas Bonnefon po::command_line_style::allow_guessing) | 103bb02e0acSNicolas Bonnefon po::command_line_style::allow_long_disguise) ^ 104bb02e0acSNicolas Bonnefon po::command_line_style::allow_sticky); 105bb02e0acSNicolas Bonnefon 106bb02e0acSNicolas Bonnefon po::variables_map vm; 107bb02e0acSNicolas Bonnefon po::store(po::command_line_parser(argc, argv). 108bb02e0acSNicolas Bonnefon options(all_options). 109bb02e0acSNicolas Bonnefon positional(positional). 110bb02e0acSNicolas Bonnefon style(command_line_style).run(), 111bb02e0acSNicolas Bonnefon vm); 112bb02e0acSNicolas Bonnefon po::notify(vm); 113bb02e0acSNicolas Bonnefon 114bb02e0acSNicolas Bonnefon if ( vm.count("help") ) { 115bb02e0acSNicolas Bonnefon desc.print(cout); 116bb02e0acSNicolas Bonnefon return 0; 117bb02e0acSNicolas Bonnefon } 118bb02e0acSNicolas Bonnefon 119bb02e0acSNicolas Bonnefon if ( vm.count("version") ) { 120bb02e0acSNicolas Bonnefon print_version(); 121bb02e0acSNicolas Bonnefon return 0; 122bb02e0acSNicolas Bonnefon } 123bb02e0acSNicolas Bonnefon 124da4beab3SNicolas Bonnefon if ( vm.count( "debug" ) ) 125bb02e0acSNicolas Bonnefon logLevel = logINFO; 126da4beab3SNicolas Bonnefon 127da4beab3SNicolas Bonnefon if ( vm.count( "multi" ) ) 128da4beab3SNicolas Bonnefon multi_instance = true; 129da4beab3SNicolas Bonnefon 130da4beab3SNicolas Bonnefon if ( vm.count( "new-session" ) ) 131da4beab3SNicolas Bonnefon new_session = true; 132bb02e0acSNicolas Bonnefon 133*1e522573SNicolas Bonnefon if ( vm.count( "load-session" ) ) 134*1e522573SNicolas Bonnefon load_session = true; 135*1e522573SNicolas Bonnefon 136320f9110SNicolas Bonnefon #ifdef _WIN32 137320f9110SNicolas Bonnefon if ( vm.count( "log" ) ) 138320f9110SNicolas Bonnefon log_to_file = true; 139320f9110SNicolas Bonnefon #endif 140320f9110SNicolas Bonnefon 141bb02e0acSNicolas Bonnefon for ( string s = "dd"; s.length() <= 10; s.append("d") ) 142bb02e0acSNicolas Bonnefon if ( vm.count( s ) ) 143bb02e0acSNicolas Bonnefon logLevel = (TLogLevel) (logWARNING + s.length()); 144bb02e0acSNicolas Bonnefon 145bb02e0acSNicolas Bonnefon if ( vm.count("input-file") ) 146bb02e0acSNicolas Bonnefon filename = vm["input-file"].as<string>(); 147bb02e0acSNicolas Bonnefon } 148bb02e0acSNicolas Bonnefon catch(exception& e) { 149bb02e0acSNicolas Bonnefon cerr << "Option processing error: " << e.what() << endl; 150bb02e0acSNicolas Bonnefon return 1; 151bb02e0acSNicolas Bonnefon } 152bb02e0acSNicolas Bonnefon catch(...) { 153bb02e0acSNicolas Bonnefon cerr << "Exception of unknown type!\n"; 154bb02e0acSNicolas Bonnefon } 155bb02e0acSNicolas Bonnefon 156320f9110SNicolas Bonnefon #ifdef _WIN32 157320f9110SNicolas Bonnefon if ( log_to_file ) 158320f9110SNicolas Bonnefon { 159320f9110SNicolas Bonnefon char file_name[255]; 160320f9110SNicolas Bonnefon snprintf( file_name, sizeof file_name, "glogg_%d.log", getpid() ); 161320f9110SNicolas Bonnefon FILE* file = fopen(file_name, "w"); 162bb02e0acSNicolas Bonnefon Output2FILE::Stream() = file; 163320f9110SNicolas Bonnefon } 164bb02e0acSNicolas Bonnefon #endif 165bb02e0acSNicolas Bonnefon 166bb02e0acSNicolas Bonnefon FILELog::setReportingLevel( logLevel ); 167bb02e0acSNicolas Bonnefon 168e07da50aSNicolas Bonnefon // External communicator 169da4beab3SNicolas Bonnefon shared_ptr<ExternalCommunicator> externalCommunicator = nullptr; 170da4beab3SNicolas Bonnefon shared_ptr<ExternalInstance> externalInstance = nullptr; 171da4beab3SNicolas Bonnefon 172da4beab3SNicolas Bonnefon try { 17315af0893SNicolas Bonnefon #ifdef GLOGG_SUPPORTS_DBUS 174da4beab3SNicolas Bonnefon externalCommunicator = make_shared<DBusExternalCommunicator>(); 175da4beab3SNicolas Bonnefon externalInstance = shared_ptr<ExternalInstance>( 176da4beab3SNicolas Bonnefon externalCommunicator->otherInstance() ); 17724895119SNicolas Bonnefon #elif GLOGG_SUPPORTS_WINIPC 17824895119SNicolas Bonnefon externalCommunicator = make_shared<WinExternalCommunicator>(); 17924895119SNicolas Bonnefon externalInstance = shared_ptr<ExternalInstance>( 18024895119SNicolas Bonnefon externalCommunicator->otherInstance() ); 18115af0893SNicolas Bonnefon #endif 182da4beab3SNicolas Bonnefon } 183da4beab3SNicolas Bonnefon catch(CantCreateExternalErr& e) { 184da4beab3SNicolas Bonnefon LOG(logWARNING) << "Cannot initialise external communication."; 185da4beab3SNicolas Bonnefon } 186557fb9d8SNicolas Bonnefon 187e07da50aSNicolas Bonnefon LOG(logDEBUG) << "externalInstance = " << externalInstance; 188da4beab3SNicolas Bonnefon if ( ( ! multi_instance ) && externalInstance ) { 189e07da50aSNicolas Bonnefon uint32_t version = externalInstance->getVersion(); 190e07da50aSNicolas Bonnefon LOG(logINFO) << "Found another glogg (version = " 191e07da50aSNicolas Bonnefon << std::setbase(16) << version << ")"; 192e07da50aSNicolas Bonnefon 19309aff35dSNicolas Bonnefon externalInstance->loadFile( QString::fromStdString( filename ) ); 19409aff35dSNicolas Bonnefon 195e07da50aSNicolas Bonnefon return 0; 196e07da50aSNicolas Bonnefon } 197e07da50aSNicolas Bonnefon else { 198e07da50aSNicolas Bonnefon // FIXME: there is a race condition here. One glogg could start 199e07da50aSNicolas Bonnefon // between the declaration of externalInstance and here, 200e07da50aSNicolas Bonnefon // is it a problem? 201da4beab3SNicolas Bonnefon if ( externalCommunicator ) 202e07da50aSNicolas Bonnefon externalCommunicator->startListening(); 203e07da50aSNicolas Bonnefon } 204e07da50aSNicolas Bonnefon 205812146a8SNicolas Bonnefon // Register types for Qt 206812146a8SNicolas Bonnefon qRegisterMetaType<LoadingStatus>("LoadingStatus"); 207812146a8SNicolas Bonnefon 208bb02e0acSNicolas Bonnefon // Register the configuration items 209bb02e0acSNicolas Bonnefon GetPersistentInfo().migrateAndInit(); 210bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 21111582726SNicolas Bonnefon std::make_shared<SessionInfo>(), QString( "session" ) ); 212bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 21311582726SNicolas Bonnefon std::make_shared<Configuration>(), QString( "settings" ) ); 214bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 21511582726SNicolas Bonnefon std::make_shared<FilterSet>(), QString( "filterSet" ) ); 216bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 21711582726SNicolas Bonnefon std::make_shared<SavedSearches>(), QString( "savedSearches" ) ); 218bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 21911582726SNicolas Bonnefon std::make_shared<RecentFiles>(), QString( "recentFiles" ) ); 220a3f81147SNicolas Bonnefon #ifdef GLOGG_SUPPORTS_VERSION_CHECKING 221a3f81147SNicolas Bonnefon GetPersistentInfo().registerPersistable( 222a3f81147SNicolas Bonnefon std::make_shared<VersionCheckerConfig>(), QString( "versionChecker" ) ); 223a3f81147SNicolas Bonnefon #endif 224bb02e0acSNicolas Bonnefon 225bb02e0acSNicolas Bonnefon // FIXME: should be replaced by a two staged init of MainWindow 226bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "settings" ) ); 227bb02e0acSNicolas Bonnefon 228d96f3f21SNicolas Bonnefon std::unique_ptr<Session> session( new Session() ); 22909aff35dSNicolas Bonnefon MainWindow mw( std::move( session ), externalCommunicator ); 230bb02e0acSNicolas Bonnefon 231bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "MainWindow created."; 2320f9fd9edSNicolas Bonnefon mw.show(); 233*1e522573SNicolas Bonnefon // Load the existing session if needed 234*1e522573SNicolas Bonnefon if ( load_session || ( filename.empty() && !new_session ) ) 2350f9fd9edSNicolas Bonnefon mw.reloadSession(); 2360f9fd9edSNicolas Bonnefon mw.loadInitialFile( QString::fromStdString( filename ) ); 2378a9275b2SNicolas Bonnefon mw.startBackgroundTasks(); 238bb02e0acSNicolas Bonnefon return app.exec(); 239bb02e0acSNicolas Bonnefon } 240bb02e0acSNicolas Bonnefon 241bb02e0acSNicolas Bonnefon static void print_version() 242bb02e0acSNicolas Bonnefon { 243bb02e0acSNicolas Bonnefon cout << "glogg " GLOGG_VERSION "\n"; 244bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 245bb02e0acSNicolas Bonnefon cout << "Built " GLOGG_DATE " from " GLOGG_COMMIT "\n"; 246bb02e0acSNicolas Bonnefon #endif 2478a9275b2SNicolas Bonnefon cout << "Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Nicolas Bonnefon and other contributors\n"; 248bb02e0acSNicolas Bonnefon cout << "This is free software. You may redistribute copies of it under the terms of\n"; 249bb02e0acSNicolas Bonnefon cout << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"; 250bb02e0acSNicolas Bonnefon cout << "There is NO WARRANTY, to the extent permitted by law.\n"; 251bb02e0acSNicolas Bonnefon } 252