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 31bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 32bb02e0acSNicolas Bonnefon #include "sessioninfo.h" 33bb02e0acSNicolas Bonnefon #include "configuration.h" 34bb02e0acSNicolas Bonnefon #include "filterset.h" 35bb02e0acSNicolas Bonnefon #include "recentfiles.h" 36d96f3f21SNicolas Bonnefon #include "session.h" 37bb02e0acSNicolas Bonnefon #include "mainwindow.h" 38bb02e0acSNicolas Bonnefon #include "savedsearches.h" 39812146a8SNicolas Bonnefon #include "loadingstatus.h" 40*15af0893SNicolas Bonnefon 41*15af0893SNicolas Bonnefon #include "externalcom.h" 42*15af0893SNicolas Bonnefon #ifdef GLOGG_SUPPORTS_DBUS 43557fb9d8SNicolas Bonnefon #include "dbusexternalcom.h" 44*15af0893SNicolas Bonnefon #endif 45*15af0893SNicolas Bonnefon 46bb02e0acSNicolas Bonnefon #include "log.h" 47bb02e0acSNicolas Bonnefon 48bb02e0acSNicolas Bonnefon static void print_version(); 49bb02e0acSNicolas Bonnefon 50bb02e0acSNicolas Bonnefon int main(int argc, char *argv[]) 51bb02e0acSNicolas Bonnefon { 52bb02e0acSNicolas Bonnefon QApplication app(argc, argv); 53bb02e0acSNicolas Bonnefon 54bb02e0acSNicolas Bonnefon string filename = ""; 55bb02e0acSNicolas Bonnefon 56da4beab3SNicolas Bonnefon // Configuration 57da4beab3SNicolas Bonnefon bool new_session = false; 58da4beab3SNicolas Bonnefon bool multi_instance = false; 59da4beab3SNicolas Bonnefon 60bb02e0acSNicolas Bonnefon TLogLevel logLevel = logWARNING; 61bb02e0acSNicolas Bonnefon 62bb02e0acSNicolas Bonnefon try { 63bb02e0acSNicolas Bonnefon po::options_description desc("Usage: glogg [options] [file]"); 64bb02e0acSNicolas Bonnefon desc.add_options() 65bb02e0acSNicolas Bonnefon ("help,h", "print out program usage (this message)") 66bb02e0acSNicolas Bonnefon ("version,v", "print glogg's version information") 67da4beab3SNicolas Bonnefon ("multi,m", "allow multiple instance of glogg to run simultaneously (use together with -s)") 68da4beab3SNicolas Bonnefon ("new-session,s", "do not load the previous session") 69bb02e0acSNicolas Bonnefon ("debug,d", "output more debug (include multiple times for more verbosity e.g. -dddd") 70bb02e0acSNicolas Bonnefon ; 71bb02e0acSNicolas Bonnefon po::options_description desc_hidden("Hidden options"); 72bb02e0acSNicolas Bonnefon // For -dd, -ddd... 73bb02e0acSNicolas Bonnefon for ( string s = "dd"; s.length() <= 10; s.append("d") ) 74bb02e0acSNicolas Bonnefon desc_hidden.add_options()(s.c_str(), "debug"); 75bb02e0acSNicolas Bonnefon 76bb02e0acSNicolas Bonnefon desc_hidden.add_options() 77bb02e0acSNicolas Bonnefon ("input-file", po::value<string>(), "input file") 78bb02e0acSNicolas Bonnefon ; 79bb02e0acSNicolas Bonnefon 80bb02e0acSNicolas Bonnefon po::options_description all_options("all options"); 81bb02e0acSNicolas Bonnefon all_options.add(desc).add(desc_hidden); 82bb02e0acSNicolas Bonnefon 83bb02e0acSNicolas Bonnefon po::positional_options_description positional; 84bb02e0acSNicolas Bonnefon positional.add("input-file", 1); 85bb02e0acSNicolas Bonnefon 86bb02e0acSNicolas Bonnefon int command_line_style = (((po::command_line_style::unix_style ^ 87bb02e0acSNicolas Bonnefon po::command_line_style::allow_guessing) | 88bb02e0acSNicolas Bonnefon po::command_line_style::allow_long_disguise) ^ 89bb02e0acSNicolas Bonnefon po::command_line_style::allow_sticky); 90bb02e0acSNicolas Bonnefon 91bb02e0acSNicolas Bonnefon po::variables_map vm; 92bb02e0acSNicolas Bonnefon po::store(po::command_line_parser(argc, argv). 93bb02e0acSNicolas Bonnefon options(all_options). 94bb02e0acSNicolas Bonnefon positional(positional). 95bb02e0acSNicolas Bonnefon style(command_line_style).run(), 96bb02e0acSNicolas Bonnefon vm); 97bb02e0acSNicolas Bonnefon po::notify(vm); 98bb02e0acSNicolas Bonnefon 99bb02e0acSNicolas Bonnefon if ( vm.count("help") ) { 100bb02e0acSNicolas Bonnefon desc.print(cout); 101bb02e0acSNicolas Bonnefon return 0; 102bb02e0acSNicolas Bonnefon } 103bb02e0acSNicolas Bonnefon 104bb02e0acSNicolas Bonnefon if ( vm.count("version") ) { 105bb02e0acSNicolas Bonnefon print_version(); 106bb02e0acSNicolas Bonnefon return 0; 107bb02e0acSNicolas Bonnefon } 108bb02e0acSNicolas Bonnefon 109da4beab3SNicolas Bonnefon if ( vm.count( "debug" ) ) 110bb02e0acSNicolas Bonnefon logLevel = logINFO; 111da4beab3SNicolas Bonnefon 112da4beab3SNicolas Bonnefon if ( vm.count( "multi" ) ) 113da4beab3SNicolas Bonnefon multi_instance = true; 114da4beab3SNicolas Bonnefon 115da4beab3SNicolas Bonnefon if ( vm.count( "new-session" ) ) 116da4beab3SNicolas Bonnefon new_session = true; 117bb02e0acSNicolas Bonnefon 118bb02e0acSNicolas Bonnefon for ( string s = "dd"; s.length() <= 10; s.append("d") ) 119bb02e0acSNicolas Bonnefon if ( vm.count( s ) ) 120bb02e0acSNicolas Bonnefon logLevel = (TLogLevel) (logWARNING + s.length()); 121bb02e0acSNicolas Bonnefon 122bb02e0acSNicolas Bonnefon if ( vm.count("input-file") ) 123bb02e0acSNicolas Bonnefon filename = vm["input-file"].as<string>(); 124bb02e0acSNicolas Bonnefon } 125bb02e0acSNicolas Bonnefon catch(exception& e) { 126bb02e0acSNicolas Bonnefon cerr << "Option processing error: " << e.what() << endl; 127bb02e0acSNicolas Bonnefon return 1; 128bb02e0acSNicolas Bonnefon } 129bb02e0acSNicolas Bonnefon catch(...) { 130bb02e0acSNicolas Bonnefon cerr << "Exception of unknown type!\n"; 131bb02e0acSNicolas Bonnefon } 132bb02e0acSNicolas Bonnefon 133bb02e0acSNicolas Bonnefon #if 0 134bb02e0acSNicolas Bonnefon FILE* file = fopen("glogg.log", "w"); 135bb02e0acSNicolas Bonnefon Output2FILE::Stream() = file; 136bb02e0acSNicolas Bonnefon #endif 137bb02e0acSNicolas Bonnefon 138bb02e0acSNicolas Bonnefon FILELog::setReportingLevel( logLevel ); 139bb02e0acSNicolas Bonnefon 140e07da50aSNicolas Bonnefon // External communicator 141da4beab3SNicolas Bonnefon shared_ptr<ExternalCommunicator> externalCommunicator = nullptr; 142da4beab3SNicolas Bonnefon shared_ptr<ExternalInstance> externalInstance = nullptr; 143da4beab3SNicolas Bonnefon 144da4beab3SNicolas Bonnefon try { 145*15af0893SNicolas Bonnefon #ifdef GLOGG_SUPPORTS_DBUS 146da4beab3SNicolas Bonnefon externalCommunicator = make_shared<DBusExternalCommunicator>(); 147da4beab3SNicolas Bonnefon externalInstance = shared_ptr<ExternalInstance>( 148da4beab3SNicolas Bonnefon externalCommunicator->otherInstance() ); 149*15af0893SNicolas Bonnefon #endif 150da4beab3SNicolas Bonnefon } 151da4beab3SNicolas Bonnefon catch(CantCreateExternalErr& e) { 152da4beab3SNicolas Bonnefon LOG(logWARNING) << "Cannot initialise external communication."; 153da4beab3SNicolas Bonnefon } 154557fb9d8SNicolas Bonnefon 155e07da50aSNicolas Bonnefon LOG(logDEBUG) << "externalInstance = " << externalInstance; 156da4beab3SNicolas Bonnefon if ( ( ! multi_instance ) && externalInstance ) { 157e07da50aSNicolas Bonnefon uint32_t version = externalInstance->getVersion(); 158e07da50aSNicolas Bonnefon LOG(logINFO) << "Found another glogg (version = " 159e07da50aSNicolas Bonnefon << std::setbase(16) << version << ")"; 160e07da50aSNicolas Bonnefon 16109aff35dSNicolas Bonnefon externalInstance->loadFile( QString::fromStdString( filename ) ); 16209aff35dSNicolas Bonnefon 163e07da50aSNicolas Bonnefon return 0; 164e07da50aSNicolas Bonnefon } 165e07da50aSNicolas Bonnefon else { 166e07da50aSNicolas Bonnefon // FIXME: there is a race condition here. One glogg could start 167e07da50aSNicolas Bonnefon // between the declaration of externalInstance and here, 168e07da50aSNicolas Bonnefon // is it a problem? 169da4beab3SNicolas Bonnefon if ( externalCommunicator ) 170e07da50aSNicolas Bonnefon externalCommunicator->startListening(); 171e07da50aSNicolas Bonnefon } 172e07da50aSNicolas Bonnefon 173812146a8SNicolas Bonnefon // Register types for Qt 174812146a8SNicolas Bonnefon qRegisterMetaType<LoadingStatus>("LoadingStatus"); 175812146a8SNicolas Bonnefon 176bb02e0acSNicolas Bonnefon // Register the configuration items 177bb02e0acSNicolas Bonnefon GetPersistentInfo().migrateAndInit(); 178bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 17911582726SNicolas Bonnefon std::make_shared<SessionInfo>(), QString( "session" ) ); 180bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 18111582726SNicolas Bonnefon std::make_shared<Configuration>(), QString( "settings" ) ); 182bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 18311582726SNicolas Bonnefon std::make_shared<FilterSet>(), QString( "filterSet" ) ); 184bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 18511582726SNicolas Bonnefon std::make_shared<SavedSearches>(), QString( "savedSearches" ) ); 186bb02e0acSNicolas Bonnefon GetPersistentInfo().registerPersistable( 18711582726SNicolas Bonnefon std::make_shared<RecentFiles>(), QString( "recentFiles" ) ); 188bb02e0acSNicolas Bonnefon 189bb02e0acSNicolas Bonnefon // FIXME: should be replaced by a two staged init of MainWindow 190bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "settings" ) ); 191bb02e0acSNicolas Bonnefon 192d96f3f21SNicolas Bonnefon std::unique_ptr<Session> session( new Session() ); 19309aff35dSNicolas Bonnefon MainWindow mw( std::move( session ), externalCommunicator ); 194bb02e0acSNicolas Bonnefon 195bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "MainWindow created."; 1960f9fd9edSNicolas Bonnefon mw.show(); 197da4beab3SNicolas Bonnefon if ( ! new_session ) 1980f9fd9edSNicolas Bonnefon mw.reloadSession(); 1990f9fd9edSNicolas Bonnefon mw.loadInitialFile( QString::fromStdString( filename ) ); 200bb02e0acSNicolas Bonnefon return app.exec(); 201bb02e0acSNicolas Bonnefon } 202bb02e0acSNicolas Bonnefon 203bb02e0acSNicolas Bonnefon static void print_version() 204bb02e0acSNicolas Bonnefon { 205bb02e0acSNicolas Bonnefon cout << "glogg " GLOGG_VERSION "\n"; 206bb02e0acSNicolas Bonnefon #ifdef GLOGG_COMMIT 207bb02e0acSNicolas Bonnefon cout << "Built " GLOGG_DATE " from " GLOGG_COMMIT "\n"; 208bb02e0acSNicolas Bonnefon #endif 209d96f3f21SNicolas Bonnefon cout << "Copyright (C) 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors\n"; 210bb02e0acSNicolas Bonnefon cout << "This is free software. You may redistribute copies of it under the terms of\n"; 211bb02e0acSNicolas Bonnefon cout << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"; 212bb02e0acSNicolas Bonnefon cout << "There is NO WARRANTY, to the extent permitted by law.\n"; 213bb02e0acSNicolas Bonnefon } 214