File indexing completed on 2024-05-19 05:21:53

0001 /*
0002  * Copyright (C) 1997 by Stephan Kulow <coolo@kde.org>
0003  * Copyright (C) 2019  Alexander Potashev <aspotashev@gmail.com>
0004  *
0005  *   This program is free software; you can redistribute it and/or modify
0006  *   it under the terms of the GNU General Public License as published by
0007  *   the Free Software Foundation; either version 2 of the License, or
0008  *   (at your option) any later version.
0009  *
0010  *   This program is distributed in the hope that it will be useful,
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *   GNU General Public License for more details.
0014  *
0015  *   You should have received a copy of the GNU General Public License along
0016  *   with this program; if not, write to the
0017  *      Free Software Foundation, Inc.
0018  *      51 Franklin Street, Fifth Floor
0019  *      Boston, MA  02110-1301  USA.
0020  *
0021  */
0022 
0023 #include <QApplication>
0024 #include <QCommandLineOption>
0025 #include <QCommandLineParser>
0026 #include <QDir>
0027 #include <QFile>
0028 #include <QFileInfo>
0029 #include <QPointer>
0030 #include <QStandardPaths>
0031 
0032 #include <KAboutData>
0033 #include <KDBusService>
0034 #include <KLocalizedString>
0035 
0036 #include "desktoplist.h"
0037 #include "ktimetracker-version.h"
0038 #include "ktt_debug.h"
0039 #include "mainwindow.h"
0040 #include "model/task.h"
0041 #include "timetrackerstorage.h"
0042 
0043 // Deliver the path/URL to the iCalendar file to be used
0044 QUrl getFileUrl(const QCommandLineParser &parser)
0045 {
0046     // Get first positional argument ("file")
0047     const QStringList args = parser.positionalArguments();
0048     QString customFile;
0049     if (!args.isEmpty()) {
0050         customFile = args[0];
0051     }
0052 
0053     if (!customFile.isEmpty()) {
0054         // customFile is given as parameter
0055 
0056         const QUrl url = QUrl::fromUserInput(customFile, QDir::currentPath(), QUrl::AssumeLocalFile);
0057         if (!url.isValid()) {
0058             qCWarning(KTT_LOG) << "Invalid URL: " << customFile;
0059         }
0060 
0061         return url;
0062     } else {
0063         // customFile is not given as parameter
0064         QString result =
0065             QString(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("ktimetracker/ktimetracker.ics")));
0066         if (result.isEmpty()) {
0067             result = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QLatin1Char('/')
0068                 + QStringLiteral("ktimetracker.ics");
0069 
0070             QFileInfo fileInfo(result);
0071             QDir().mkpath(fileInfo.absolutePath());
0072 
0073             QFile oldFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("karm/karm.ics")));
0074             if (oldFile.exists()) {
0075                 oldFile.copy(result);
0076             }
0077         }
0078 
0079         return QUrl::fromLocalFile(result);
0080     }
0081 }
0082 
0083 int main(int argc, char *argv[])
0084 {
0085     QApplication app(argc, argv);
0086     Q_INIT_RESOURCE(ktimetracker);
0087 
0088 #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
0089     // Force Breeze theme on Windows.
0090     // Kate text editor does the same.
0091     QApplication::setStyle(QStringLiteral("breeze"));
0092 #endif
0093 
0094     KLocalizedString::setApplicationDomain("ktimetracker");
0095 
0096     KAboutData aboutData(QStringLiteral("ktimetracker"),
0097                          i18n("KTimeTracker"),
0098                          QStringLiteral(KTIMETRACKER_VERSION_STRING),
0099                          i18n("KDE Time tracker tool"),
0100                          KAboutLicense::GPL_V2,
0101                          i18n("Copyright © 1997-2019 KTimeTracker developers"),
0102                          QString(),
0103                          QStringLiteral("https://userbase.kde.org/KTimeTracker"));
0104 
0105     aboutData.addAuthor(i18nc("@info:credit", "Alexander Potashev"),
0106                         i18nc("@info:credit", "Current Maintainer (since 2019)"),
0107                         QStringLiteral("aspotashev@gmail.com"));
0108     aboutData.addAuthor(i18nc("@info:credit", "Thorsten Stärk"),
0109                         i18nc("@info:credit", "Maintainer (2006-2012)"),
0110                         QStringLiteral("kde@staerk.de"));
0111     aboutData.addAuthor(i18nc("@info:credit", "Mark Bucciarelli"),
0112                         i18nc("@info:credit", "Maintainer (2005-2006)"),
0113                         QStringLiteral("mark@hubcapconsulting.com"));
0114     aboutData.addAuthor(i18nc("@info:credit", "Jesper Pedersen"),
0115                         i18nc("@info:credit", "Maintainer (2000-2005)"),
0116                         QStringLiteral("blackie@kde.org"));
0117     aboutData.addAuthor(i18nc("@info:credit", "Sirtaj Singh Kang"),
0118                         i18nc("@info:credit", "Original Author"),
0119                         QStringLiteral("taj@kde.org"));
0120     aboutData.addAuthor(i18nc("@info:credit", "Mathias Soeken"),
0121                         i18nc("@info:credit", "Developer (in 2007)"),
0122                         QStringLiteral("msoeken@tzi.de"));
0123     aboutData.addAuthor(i18nc("@info:credit", "Kalle Dalheimer"),
0124                         i18nc("@info:credit", "Developer (1999-2000)"),
0125                         QStringLiteral("kalle@kde.org"));
0126     aboutData.addAuthor(i18nc("@info:credit", "Allen Winter"),
0127                         i18nc("@info:credit", "Developer"),
0128                         QStringLiteral("winter@kde.org"));
0129     aboutData.addAuthor(i18nc("@info:credit", "David Faure"),
0130                         i18nc("@info:credit", "Developer"),
0131                         QStringLiteral("faure@kde.org"));
0132     KAboutData::setApplicationData(aboutData);
0133 
0134     Q_INIT_RESOURCE(icons);
0135     QIcon::setThemeSearchPaths(QStringList() << QStringLiteral(":/icons"));
0136     QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("ktimetracker")));
0137 
0138     QCommandLineParser parser;
0139     aboutData.setupCommandLine(&parser);
0140 
0141     parser.addPositionalArgument(QStringLiteral("url"), i18nc("@info:shell", "Path or URL to iCalendar file to open."));
0142 
0143     parser.process(app);
0144     aboutData.processCommandLine(&parser);
0145 
0146     KDBusService dbusService(KDBusService::Unique);
0147 
0148     const QUrl &url = getFileUrl(parser);
0149 
0150 //        if (!KUniqueApplication::start()) {
0151 //            qCDebug(KTT_LOG) << "Other instance is already running, exiting!";
0152 //            return 0;
0153 //        }
0154 //        KUniqueApplication myApp;
0155     QPointer<MainWindow> mainWindow = new MainWindow(url);
0156     mainWindow->show();
0157 
0158     if (app.isSessionRestored()) {
0159         const QString className = KXmlGuiWindow::classNameOfToplevel(1);
0160         if (className == QLatin1String("MainWindow")) {
0161             mainWindow->restore(1);
0162         } else {
0163             qCWarning(KTT_LOG) << "Unknown class " << className << " in session saved data!";
0164         }
0165     }
0166 
0167     return app.exec();
0168 }