File indexing completed on 2022-09-27 16:29:02

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 by Sébastien Laoût <slaout@linux62.org>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #include "application.h"
0007 
0008 #include <QCommandLineParser>
0009 #include <QDir>
0010 #include <QFile>
0011 #include <QFileInfo>
0012 #include <QString>
0013 #include <QTimer>
0014 
0015 #include <KLocalizedString>
0016 
0017 #include "aboutdata.h"
0018 #include "bnpview.h"
0019 #include "config.h"
0020 #include "global.h"
0021 #include "mainwindow.h"
0022 
0023 #ifdef WITH_LIBGIT2
0024 extern "C" {
0025 #include <git2.h>
0026 }
0027 #endif
0028 
0029 Application::Application(int &argc, char **argv)
0030     : QApplication(argc, argv)
0031     , m_service(KDBusService::Unique)
0032 {
0033     KLocalizedString::setApplicationDomain("basket");
0034     KAboutData::setApplicationData(AboutData());
0035     // BasketPart::createAboutData();
0036 
0037     connect(&m_service, &KDBusService::activateRequested, this, &Application::onActivateRequested);
0038 
0039 #ifdef WITH_LIBGIT2
0040 #if LIBGIT2_VER_MAJOR > 0 || (LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR >= 22)
0041     git_libgit2_init();
0042 #else
0043     git_threads_init();
0044 #endif
0045 #endif
0046 }
0047 
0048 Application::~Application()
0049 {
0050 #ifdef WITH_LIBGIT2
0051 #if LIBGIT2_VER_MAJOR > 0 || (LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR >= 22)
0052     git_libgit2_shutdown();
0053 #else
0054     git_threads_shutdown();
0055 #endif
0056 #endif
0057 }
0058 
0059 void Application::tryLoadFile(const QStringList &args, const QString &workingDir)
0060 {
0061     // Open the basket archive or template file supplied as argument:
0062     if (args.count() >= 1) {
0063         QString fileName = QDir(workingDir).filePath(args.last());
0064 
0065         if (QFile::exists(fileName)) {
0066             QFileInfo fileInfo(fileName);
0067             if (fileInfo.absoluteFilePath().contains(Global::basketsFolder())) {
0068                 QString folder = fileInfo.absolutePath().split('/').last();
0069                 folder.append("/");
0070                 BNPView::s_basketToOpen = folder;
0071                 QTimer::singleShot(100, Global::bnpView, SLOT(delayedOpenBasket()));
0072             } else if (!fileInfo.isDir()) { // Do not mis-interpret data-folder param!
0073                 // Tags are not loaded until Global::bnpView::lateInit() is called.
0074                 // It is called 0ms after the application start.
0075                 BNPView::s_fileToOpen = fileName;
0076                 QTimer::singleShot(100, Global::bnpView, SLOT(delayedOpenArchive()));
0077                 //              Global::bnpView->openArchive(fileName);
0078             }
0079         }
0080     }
0081 }
0082 
0083 void Application::onActivateRequested(const QStringList &args, const QString &workingDir)
0084 {
0085     if (MainWindow *wnd = Global::mainWindow()) {
0086         // Restore window:
0087         wnd->show();                           // from tray
0088         wnd->setWindowState(Qt::WindowActive); // from minimized
0089         // Raise to the top
0090         wnd->raise();
0091     }
0092     tryLoadFile(args, workingDir);
0093 }