File indexing completed on 2024-04-28 04:44:34

0001 #include <QCommandLineParser>
0002 #include <QIcon>
0003 #include <QQmlApplicationEngine>
0004 #include <QQmlContext>
0005 #include <QDir>
0006 
0007 #include <KI18n/KLocalizedString>
0008 
0009 #include <MauiKit3/Core/mauiapp.h>
0010 #include <MauiKit3/TextEditor/moduleinfo.h>
0011 
0012 #ifdef Q_OS_ANDROID
0013 #include <QGuiApplication>
0014 #include <MauiKit3/Core/mauiandroid.h>
0015 #else
0016 #include <QApplication>
0017 #endif
0018 
0019 #include "../buho_version.h"
0020 
0021 #include "owl.h"
0022 #include "models/notes/notes.h"
0023 #include "utils/server.h"
0024 
0025 #define BUHO_URI "org.maui.buho"
0026 
0027 static void setFolders()
0028 {
0029     QDir notes_path(OWL::NotesPath.toLocalFile());
0030     if (!notes_path.exists())
0031         notes_path.mkpath(".");
0032 }
0033 
0034 int Q_DECL_EXPORT main(int argc, char *argv[])
0035 {
0036     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
0037     QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0038 
0039 #ifdef Q_OS_ANDROID
0040     QGuiApplication app(argc, argv);
0041     if (!MAUIAndroid::checkRunTimePermissions({"android.permission.WRITE_EXTERNAL_STORAGE"}))
0042         return -1;
0043 #else
0044     QApplication app(argc, argv);
0045 #endif
0046 
0047     setFolders ();
0048 
0049     app.setOrganizationName(QStringLiteral("Maui"));
0050     app.setWindowIcon(QIcon(":/buho.png"));
0051 
0052     KLocalizedString::setApplicationDomain("buho");
0053     KAboutData about(QStringLiteral("buho"),
0054                      QStringLiteral("Buho"),
0055                      BUHO_VERSION_STRING,
0056                      i18n("Create and organize your notes."),
0057                      KAboutLicense::LGPL_V3,
0058                      APP_COPYRIGHT_NOTICE,
0059                      QString(GIT_BRANCH) + "/" + QString(GIT_COMMIT_HASH));
0060 
0061     about.addAuthor(QStringLiteral("Camilo Higuita"), i18n("Developer"), QStringLiteral("milo.h@aol.com"));
0062     about.setHomepage("https://mauikit.org");
0063     about.setProductName("maui/buho");
0064     about.setBugAddress("https://invent.kde.org/maui/buho/-/issues");
0065     about.setOrganizationDomain(BUHO_URI);
0066     about.setProgramLogo(app.windowIcon());
0067 
0068     const auto FBData = MauiKitTextEditor::aboutData();
0069     about.addComponent(FBData.name(), MauiKitTextEditor::buildVersion(), FBData.version(), FBData.webAddress());
0070 
0071     KAboutData::setApplicationData(about);
0072     MauiApp::instance()->setIconName("qrc:/buho.svg");
0073 
0074     QCommandLineOption newNoteOption(QStringList() << "n" << "new", "Create a new note.");
0075     QCommandLineOption newNoteContent(QStringList() << "c" << "content", "new note contents.", "content");
0076 
0077     QCommandLineParser parser;
0078 
0079     parser.addOption(newNoteOption);
0080     parser.addOption(newNoteContent);
0081 
0082     about.setupCommandLine(&parser);
0083     parser.process(app);
0084 
0085     about.processCommandLine(&parser);
0086 
0087     bool newNote = parser.isSet(newNoteOption);
0088     QString noteContent;
0089 
0090 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID
0091 
0092     if(newNote)
0093     {
0094         if(parser.isSet(newNoteContent))
0095         {
0096             noteContent = parser.value(newNoteContent);
0097         }
0098     }
0099 
0100     if (AppInstance::attachToExistingInstance(newNote, noteContent))
0101     {
0102         // Successfully attached to existing instance of Nota
0103         return 0;
0104     }
0105 
0106     AppInstance::registerService();
0107 #endif
0108 
0109     auto server = std::make_unique<Server>();
0110 
0111     QQmlApplicationEngine engine;
0112     const QUrl url(QStringLiteral("qrc:/main.qml"));
0113     QObject::connect(
0114                 &engine,
0115                 &QQmlApplicationEngine::objectCreated,
0116                 &app,
0117                 [url, newNote, noteContent, &server](QObject *obj, const QUrl &objUrl) {
0118         if (!obj && url == objUrl)
0119             QCoreApplication::exit(-1);
0120 
0121         server->setQmlObject(obj);
0122         if(newNote)
0123         {
0124         server->newNote(noteContent);
0125         }
0126     },
0127     Qt::QueuedConnection);
0128 
0129     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0130 
0131     qmlRegisterType<Notes>(BUHO_URI, 1, 0, "Notes");
0132 
0133     engine.load(url);
0134     return app.exec();
0135 }