Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.41 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. #include "dcpp/stdinc.h"
  8. #include "dcpp/DCPlusPlus.h"
  9.  
  10. #include "dcpp/forward.h"
  11. #include "dcpp/QueueManager.h"
  12. #include "dcpp/HashManager.h"
  13. #include "dcpp/Thread.h"
  14. #include "dcpp/Singleton.h"
  15.  
  16. #ifdef __HAIKU__
  17. #include "EiskaltApp_haiku.h"
  18. #endif
  19.  
  20. #include "WulforUtil.h"
  21. #include "WulforSettings.h"
  22. #include "HubManager.h"
  23. #include "Notification.h"
  24. #include "VersionGlobal.h"
  25. #include "IPFilter.h"
  26. #include "EmoticonFactory.h"
  27. #include "FinishedTransfers.h"
  28. #include "QueuedUsers.h"
  29. #include "ArenaWidgetManager.h"
  30. #include "ArenaWidgetFactory.h"
  31. #include "MainWindow.h"
  32. #include "GlobalTimer.h"
  33.  
  34. #ifndef __HAIKU__
  35. #include "EiskaltApp.h"
  36. #endif
  37.  
  38. #ifdef USE_ASPELL
  39. #include "SpellCheck.h"
  40. #endif
  41.  
  42. #ifdef USE_JS
  43. #include "ScriptEngine.h"
  44. #endif
  45.  
  46. #include <QApplication>
  47. #include <QMainWindow>
  48. #include <QRegExp>
  49. #include <QObject>
  50. #include <QTextCodec>
  51.  
  52. #ifdef DBUS_NOTIFY
  53. #include <QtDBus>
  54. #endif
  55.  
  56. void callBack(void* x, const std::string& a)
  57. {
  58.     std::cout << QObject::tr("Loading: ").toStdString() << a << std::endl;
  59. }
  60.  
  61. void parseCmdLine(const QStringList &);
  62.  
  63. #ifndef Q_WS_WIN
  64. #include <unistd.h>
  65. #include <signal.h>
  66. #ifndef __HAIKU__
  67. #include <execinfo.h>
  68.  
  69. #ifdef ENABLE_STACKTRACE
  70. #include "extra/stacktrace.h"
  71. #endif // ENABLE_STACKTRACE
  72.  
  73. void installHandlers();
  74. #endif
  75.  
  76. #ifdef FORCE_XDG
  77. #include <QTextStream>
  78. void migrateConfig();
  79. #endif
  80.  
  81. #else //WIN32
  82. #include <locale.h>
  83. #endif
  84.  
  85. int main(int argc, char *argv[])
  86. {
  87.     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
  88.     setlocale(LC_ALL, "");
  89.  
  90.     EiskaltApp app(argc, argv, _q(dcpp::Util::getLoginName()+"EDCPP"));
  91.     int ret = 0;
  92.  
  93.     parseCmdLine(app.arguments());
  94.  
  95.     if (app.isRunning()){
  96.         QStringList args = app.arguments();
  97.         args.removeFirst();//remove path to executable
  98. #ifndef __HAIKU__
  99.         app.sendMessage(args.join("\n"));
  100. #endif
  101.         return 0;
  102.     }
  103.  
  104. #if !defined (Q_WS_WIN) && !defined (__HAIKU__)
  105.     installHandlers();
  106. #endif
  107.  
  108. #if defined(FORCE_XDG) && !defined(Q_WS_WIN)
  109.     migrateConfig();
  110. #endif
  111.  
  112.     dcpp::startup(callBack, NULL);
  113.     dcpp::TimerManager::getInstance()->start();
  114.  
  115.     HashManager::getInstance()->setPriority(Thread::IDLE);
  116.  
  117.     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
  118.  
  119.     app.setOrganizationName("EiskaltDC++ Team");
  120.     app.setApplicationName("EiskaltDC++ Qt");
  121.     app.setApplicationVersion(EISKALTDCPP_VERSION);
  122.    
  123.     GlobalTimer::newInstance();
  124.  
  125.     WulforSettings::newInstance();
  126.     WulforSettings::getInstance()->load();
  127.     WulforSettings::getInstance()->loadTranslation();
  128.     WulforSettings::getInstance()->loadTheme();
  129.  
  130.     WulforUtil::newInstance();
  131.  
  132.     Text::hubDefaultCharset = WulforUtil::getInstance()->qtEnc2DcEnc(WSGET(WS_DEFAULT_LOCALE)).toStdString();
  133.  
  134.     if (WulforUtil::getInstance()->loadUserIcons())
  135.         std::cout << QObject::tr("UserList icons has been loaded").toStdString() << std::endl;
  136.  
  137.     if (WulforUtil::getInstance()->loadIcons())
  138.         std::cout << QObject::tr("Application icons has been loaded").toStdString() << std::endl;
  139.  
  140.     app.setWindowIcon(WICON(WulforUtil::eiICON_APPL));
  141.    
  142.     ArenaWidgetManager::newInstance();
  143.  
  144.     MainWindow::newInstance();
  145.     MainWindow::getInstance()->setUnload(!WBGET(WB_TRAY_ENABLED));
  146.  
  147.     app.connect(&app, SIGNAL(messageReceived(QString)), MainWindow::getInstance(), SLOT(parseInstanceLine(QString)));
  148.  
  149.     HubManager::newInstance();
  150.  
  151.     WulforSettings::getInstance()->loadTheme();
  152.  
  153.     if (WBGET(WB_APP_ENABLE_EMOTICON)){
  154.         EmoticonFactory::newInstance();
  155.         EmoticonFactory::getInstance()->load();
  156.     }
  157.  
  158. #ifdef USE_ASPELL
  159.     if (WBGET(WB_APP_ENABLE_ASPELL))
  160.         SpellCheck::newInstance();
  161. #endif
  162.  
  163.     Notification::newInstance();
  164.  
  165. #ifdef USE_JS
  166.     ScriptEngine::newInstance();
  167.     QObject::connect(ScriptEngine::getInstance(), SIGNAL(scriptChanged(QString)), MainWindow::getInstance(), SLOT(slotJSFileChanged(QString)));
  168. #endif
  169.    
  170.     ArenaWidgetFactory().create< dcpp::Singleton, FinishedUploads >();
  171.     ArenaWidgetFactory().create< dcpp::Singleton, FinishedDownloads >();
  172.     ArenaWidgetFactory().create< dcpp::Singleton, QueuedUsers >();
  173.    
  174.     MainWindow::getInstance()->autoconnect();
  175.     MainWindow::getInstance()->parseCmdLine();
  176.    
  177.     if (!WBGET(WB_MAINWINDOW_HIDE) || !WBGET(WB_TRAY_ENABLED))
  178.         MainWindow::getInstance()->show();
  179.  
  180.     ret = app.exec();
  181.  
  182.     std::cout << QObject::tr("Shutting down libdcpp...").toStdString() << std::endl;
  183.  
  184.     WulforSettings::getInstance()->save();
  185.  
  186.     EmoticonFactory::deleteInstance();
  187.  
  188. #ifdef USE_ASPELL
  189.     if (SpellCheck::getInstance())
  190.         SpellCheck::deleteInstance();
  191. #endif
  192.     Notification::deleteInstance();
  193.  
  194. #ifdef USE_JS
  195.     ScriptEngine::deleteInstance();
  196. #endif
  197.    
  198.     ArenaWidgetManager::deleteInstance();
  199.    
  200.     HubManager::getInstance()->release();
  201.  
  202.     MainWindow::deleteInstance();
  203.  
  204.     WulforUtil::deleteInstance();
  205.     WulforSettings::deleteInstance();
  206.    
  207.     GlobalTimer::deleteInstance();
  208.  
  209.     dcpp::shutdown();
  210.  
  211.     if (IPFilter::getInstance()){
  212.         IPFilter::getInstance()->saveList();
  213.         IPFilter::deleteInstance();
  214.     }
  215.  
  216.     std::cout << QObject::tr("Quit...").toStdString() << std::endl;
  217.  
  218.     return ret;
  219. }
  220.  
  221. void parseCmdLine(const QStringList &args){
  222.     foreach (const QString &arg, args){
  223.         if (arg == "-h" || arg == "--help"){
  224.             About().printHelp();
  225.  
  226.             exit(0);
  227.         }
  228.         else if (arg == "-v" || arg == "--version"){
  229.             About().printVersion();
  230.  
  231.             exit(0);
  232.         }
  233.     }
  234. }
  235.  
  236. #if !defined (Q_WS_WIN) && !defined (__HAIKU__)
  237.  
  238. void catchSIG(int sigNum) {
  239.     psignal(sigNum, "Catching signal ");
  240.  
  241. #ifdef ENABLE_STACKTRACE
  242.     printBacktrace(sigNum);
  243. #endif // ENABLE_STACKTRACE
  244.    
  245.     EiskaltApp *eapp = dynamic_cast<EiskaltApp*>(qApp);
  246.    
  247.     if (eapp) {
  248.         eapp->getSharedMemory().unlock();
  249.         eapp->getSharedMemory().detach();
  250.     }
  251.    
  252.     raise(SIGINT);
  253.    
  254.     std::abort();
  255. }
  256.  
  257. template <int sigNum = 0, int ... Params>
  258. void catchSignals() {
  259.     if (!sigNum)
  260.         return;
  261.  
  262.     psignal(sigNum, "Installing handler for");
  263.  
  264.     signal(sigNum, catchSIG);
  265.  
  266.     catchSignals<Params ... >();
  267. }
  268.  
  269. void installHandlers(){
  270.     struct sigaction sa;
  271.     memset(&sa, 0, sizeof(sa));
  272.     sa.sa_handler = SIG_IGN;
  273.  
  274.     if (sigaction(SIGPIPE, &sa, NULL) == -1)
  275.         printf("Cannot handle SIGPIPE\n");
  276.  
  277.     catchSignals<SIGSEGV, SIGABRT, SIGBUS, SIGKILL, SIGTERM>();
  278.  
  279.     printf("Signal handlers installed.\n");
  280. }
  281.  
  282. #endif
  283.  
  284. #ifdef FORCE_XDG
  285.  
  286. void copy(const QDir &from, const QDir &to){
  287.     if (!from.exists() || to.exists())
  288.         return;
  289.  
  290.     QString to_path = to.absolutePath();
  291.     QString from_path = from.absolutePath();
  292.  
  293.     if (!to_path.endsWith(QDir::separator()))
  294.         to_path += QDir::separator();
  295.  
  296.     if (!from_path.endsWith(QDir::separator()))
  297.         from_path += QDir::separator();
  298.  
  299.     foreach (const QString &s, from.entryList(QDir::Dirs)){
  300.         QDir new_dir(to_path+s);
  301.  
  302.         if (new_dir.exists())
  303.             continue;
  304.         else{
  305.             if (!new_dir.mkpath(new_dir.absolutePath()))
  306.                 continue;
  307.  
  308.             copy(QDir(from_path+s), new_dir);
  309.         }
  310.     }
  311.  
  312.     foreach (const QString &f, from.entryList(QDir::Files)){
  313.         QFile orig(from_path+f);
  314.  
  315.         if (!orig.copy(to_path+f))
  316.             continue;
  317.     }
  318. }
  319.  
  320. void migrateConfig(){
  321.     const char* home_ = getenv("HOME");
  322.     string home = home_ ? Text::toUtf8(home_) : "/tmp/";
  323.     string old_config = home + "/.eiskaltdc++/";
  324.  
  325.     const char *xdg_config_home_ = getenv("XDG_CONFIG_HOME");
  326.     string xdg_config_home = xdg_config_home_? Text::toUtf8(xdg_config_home_) : (home+"/.config");
  327.     string new_config = xdg_config_home + "/eiskaltdc++/";
  328.  
  329.     if (!QDir().exists(old_config.c_str()) || QDir().exists(new_config.c_str())){
  330.         if (!QDir().exists(new_config.c_str())){
  331.             old_config = _DATADIR + string("/config/");
  332.  
  333.             if (!QDir().exists(old_config.c_str()))
  334.                 return;
  335.         }
  336.         else
  337.             return;
  338.     }
  339.  
  340.     try{
  341.         printf("Migrating to XDG paths...\n");
  342.  
  343.         copy(QDir(old_config.c_str()), QDir(new_config.c_str()));
  344.  
  345.         QFile orig(new_config.c_str()+QString("DCPlusPlus.xml"));
  346.         QFile new_file(new_config.c_str()+QString("DCPlusPlus.xml.new"));
  347.  
  348.         if (!(orig.open(QIODevice::ReadOnly | QIODevice::Text) && new_file.open(QIODevice::WriteOnly | QIODevice::Text))){
  349.             orig.close();
  350.             new_file.close();
  351.  
  352.             printf("Migration failed.\n");
  353.  
  354.             return;
  355.         }
  356.  
  357.         QTextStream rstream(&orig);
  358.         QTextStream wstream(&new_file);
  359.  
  360.         QRegExp replace_str("/(\\S+)/\\.eiskaltdc\\+\\+/");
  361.         QString line = "";
  362.  
  363.         while (!rstream.atEnd()){
  364.             line = rstream.readLine();
  365.  
  366.             line.replace(replace_str, QString(new_config.c_str()));
  367.  
  368.             wstream << line << "\n";
  369.         }
  370.  
  371.         wstream.flush();
  372.  
  373.         orig.close();
  374.         new_file.close();
  375.  
  376.         orig.remove();
  377.         new_file.rename(orig.fileName());
  378.  
  379.         printf("Ok. Migrated.\n");
  380.     }
  381.     catch(const std::exception&){
  382.         printf("Migration failed.\n");
  383.     }
  384. }
  385. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement