diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 22b313b5de128..e0a1c69819d1e 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -59,6 +59,7 @@ set(client_SRCS logbrowser.cpp navigationpanehelper.cpp networksettings.cpp + ocsnavigationappsjob.cpp ocsjob.cpp ocssharejob.cpp ocsshareejob.cpp diff --git a/src/gui/ocsnavigationappsjob.cpp b/src/gui/ocsnavigationappsjob.cpp new file mode 100644 index 0000000000000..b070873d51125 --- /dev/null +++ b/src/gui/ocsnavigationappsjob.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) by Camila Ayres + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "ocsnavigationappsjob.h" + +namespace OCC { + +OcsNavigationAppsJob::OcsNavigationAppsJob(AccountPtr account) + : OcsJob(account) +{ + setPath("ocs/v2.php/core/navigation/apps"); + connect(this, &OcsNavigationAppsJob::jobFinished, this, &OcsNavigationAppsJob::jobDone); +} + +void OcsNavigationAppsJob::getNavigationApps() +{ + setVerb("GET"); + addParam("absolute", "true"); + start(); +} + +void OcsNavigationAppsJob::jobDone(const QJsonDocument &reply) +{ + + emit appsJobFinished(reply); +} +} diff --git a/src/gui/ocsnavigationappsjob.h b/src/gui/ocsnavigationappsjob.h new file mode 100644 index 0000000000000..2c0c7bd4a6c5e --- /dev/null +++ b/src/gui/ocsnavigationappsjob.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) by Camila Ayres + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef OCSNAVIGATIONAPPSJOB_H +#define OCSNAVIGATIONAPPSJOB_H + +#include "ocsjob.h" +class QJsonDocument; + +namespace OCC { + +/** + * @brief The OcsAppsJob class + * @ingroup gui + * + * Fetching enabled apps from the OCS Apps API + */ +class OcsNavigationAppsJob : public OcsJob +{ + Q_OBJECT +public: + explicit OcsNavigationAppsJob(AccountPtr account); + + /** + * Get a list of enabled apps and external sites + * visible in the Navigation menu + */ + void getNavigationApps(); + +signals: + /** + * Result of the OCS request + * + * @param reply The reply + */ + void appsJobFinished(const QJsonDocument &reply); + +private slots: + void jobDone(const QJsonDocument &reply); +}; +} + +#endif // OCSNAVIGATIONAPPSJOB_H diff --git a/src/gui/owncloudgui.cpp b/src/gui/owncloudgui.cpp index 10be7f430e302..d0d8fa940f3c3 100644 --- a/src/gui/owncloudgui.cpp +++ b/src/gui/owncloudgui.cpp @@ -14,6 +14,7 @@ #include "application.h" #include "owncloudgui.h" +#include "ocsnavigationappsjob.h" #include "theme.h" #include "folderman.h" #include "configfile.h" @@ -51,9 +52,14 @@ #include #endif +#include +#include +#include + namespace OCC { const char propertyAccountC[] = "oc_account"; +const char propertyMenuC[] = "oc_account_menu"; ownCloudGui::ownCloudGui(Application *parent) : QObject(parent) @@ -565,7 +571,6 @@ void ownCloudGui::updateContextMenu() } _accountMenus.clear(); - auto accountList = AccountManager::instance()->accounts(); bool isConfigured = (!accountList.isEmpty()); @@ -592,9 +597,11 @@ void ownCloudGui::updateContextMenu() _contextMenu->addMenu(accountMenu); addAccountContextMenu(account, accountMenu, true); + fetchNavigationApps(account, accountMenu); } } else if (accountList.count() == 1) { addAccountContextMenu(accountList.first(), _contextMenu.data(), false); + fetchNavigationApps(accountList.first(), _contextMenu.data()); } _contextMenu->addSeparator(); @@ -729,11 +736,6 @@ void ownCloudGui::setupActions() _actionQuit = new QAction(tr("Quit %1").arg(Theme::instance()->appNameGUI()), this); QObject::connect(_actionQuit, SIGNAL(triggered(bool)), _app, SLOT(quit())); - _actionLogin = new QAction(tr("Log in..."), this); - connect(_actionLogin, &QAction::triggered, this, &ownCloudGui::slotLogin); - _actionLogout = new QAction(tr("Log out"), this); - connect(_actionLogout, &QAction::triggered, this, &ownCloudGui::slotLogout); - if (_app->debugMode()) { _actionCrash = new QAction(tr("Crash now", "Only shows in debug mode to allow testing the crash handler"), this); connect(_actionCrash, &QAction::triggered, _app, &Application::slotCrash); @@ -742,6 +744,64 @@ void ownCloudGui::setupActions() } } +void ownCloudGui::fetchNavigationApps(AccountStatePtr account, QMenu *accountMenu){ + OcsNavigationAppsJob *job = new OcsNavigationAppsJob(account->account()); + job->setProperty(propertyAccountC, QVariant::fromValue(account->account())); + job->setProperty(propertyMenuC, QVariant::fromValue(accountMenu)); + connect(job, &OcsNavigationAppsJob::appsJobFinished, this, &ownCloudGui::slotNavigationAppsFetched); + connect(job, &OcsNavigationAppsJob::ocsError, this, &ownCloudGui::slotOcsError); + job->getNavigationApps(); +} + +void ownCloudGui::slotNavigationAppsFetched(const QJsonDocument &reply) +{ + if(!reply.isEmpty()){ + auto element = reply.object().value("ocs").toObject().value("data"); + auto navLinks = element.toArray(); + if(navLinks.size() > 0){ + if(auto account = qvariant_cast(sender()->property(propertyAccountC))){ + if(QMenu *accountMenu = qvariant_cast(sender()->property(propertyMenuC))){ + + // when there is only one account add the nav links above the settings + QAction *actionBefore = _actionSettings; + + // when there is more than one account add the nav links above pause/unpause folder or logout action + if(AccountManager::instance()->accounts().size() > 1){ + foreach(QAction *action, accountMenu->actions()){ + + // pause/unpause folder and logout actions have propertyAccountC + if(auto actionAccount = qvariant_cast(action->property(propertyAccountC))){ + if(actionAccount->account() == account){ + actionBefore = action; + break; + } + } + } + } + + // Create submenu with links + QMenu *navLinksMenu = new QMenu(tr("Apps")); + accountMenu->insertSeparator(actionBefore); + accountMenu->insertMenu(actionBefore, navLinksMenu); + foreach (const QJsonValue &value, navLinks) { + auto navLink = value.toObject(); + QAction *action = new QAction(navLink.value("name").toString(), this); + QUrl href(navLink.value("href").toString()); + connect(action, &QAction::triggered, this, [href] { QDesktopServices::openUrl(href); }); + navLinksMenu->addAction(action); + } + accountMenu->insertSeparator(actionBefore); + } + } + } + } +} + +void ownCloudGui::slotOcsError(int statusCode, const QString &message) +{ + emit serverError(statusCode, message); +} + void ownCloudGui::slotRebuildRecentMenus() { _recentActionsMenu->clear(); diff --git a/src/gui/owncloudgui.h b/src/gui/owncloudgui.h index cba88c1e4eeb7..432a22014fbf0 100644 --- a/src/gui/owncloudgui.h +++ b/src/gui/owncloudgui.h @@ -65,6 +65,7 @@ class ownCloudGui : public QObject signals: void setupProxy(); + void serverError(int code, const QString &message); public slots: void setupContextMenu(); @@ -92,6 +93,7 @@ public slots: void slotOpenPath(const QString &path); void slotAccountStateChanged(); void slotTrayMessageIfServerUnsupported(Account *account); + void slotNavigationAppsFetched(const QJsonDocument &reply); /** * Open a share dialog for a file or folder. @@ -104,6 +106,9 @@ public slots: void slotRemoveDestroyedShareDialogs(); +protected slots: + void slotOcsError(int statusCode, const QString &message); + private slots: void slotLogin(); void slotLogout(); @@ -115,6 +120,7 @@ private slots: void setPauseOnAllFoldersHelper(bool pause); void setupActions(); void addAccountContextMenu(AccountStatePtr accountState, QMenu *menu, bool separateMenu); + void fetchNavigationApps(AccountStatePtr account, QMenu *accountMenu); QPointer _tray; #if defined(Q_OS_MAC) @@ -140,9 +146,6 @@ private slots: QTimer _workaroundBatchTrayUpdate; QMap> _shareDialogs; - QAction *_actionLogin; - QAction *_actionLogout; - QAction *_actionNewAccountWizard; QAction *_actionSettings; QAction *_actionStatus;