File indexing completed on 2024-05-12 05:21:24

0001 /*
0002   This file is part of KOrganizer.
0003 
0004   SPDX-FileCopyrightText: 1997, 1998, 1999 Preston Brown <preston.brown@yale.edu>
0005   SPDX-FileCopyrightText: Fester Zigterman <F.J.F.ZigtermanRustenburg@student.utwente.nl>
0006   SPDX-FileCopyrightText: Ian Dawes <iadawes@globalserve.net>
0007   SPDX-FileCopyrightText: Laszlo Boloni <boloni@cs.purdue.edu>
0008 
0009   SPDX-FileCopyrightText: 2000-2003 Cornelius Schumacher <schumacher@kde.org>
0010   SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
0011 
0012   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0013 */
0014 
0015 #include "korganizer.h"
0016 #include "actionmanager.h"
0017 #include "calendarview.h"
0018 #include "impl/korganizerifaceimpl.h"
0019 #include "kocore.h"
0020 #include "koglobals.h"
0021 #include "plugininterface/korganizerplugininterface.h"
0022 
0023 #include <Libkdepim/ProgressStatusBarWidget>
0024 #include <Libkdepim/StatusbarProgressWidget>
0025 
0026 // #include "korganizer_debug.h"
0027 #include <KAboutData>
0028 #include <KActionCollection>
0029 #include <KSharedConfig>
0030 #include <KShortcutsDialog>
0031 #include <KStandardAction>
0032 #include <KToolBar>
0033 #include <PimCommon/NeedUpdateVersionUtils>
0034 #include <PimCommon/NeedUpdateVersionWidget>
0035 #include <QLabel>
0036 #include <QStatusBar>
0037 #include <QVBoxLayout>
0038 #ifdef WITH_KUSERFEEDBACK
0039 #include "userfeedback/userfeedbackmanager.h"
0040 
0041 #include <KUserFeedback/NotificationPopup>
0042 #include <KUserFeedback/Provider>
0043 #endif
0044 
0045 KOrganizer::KOrganizer()
0046     : KParts::MainWindow()
0047     , KOrg::MainWindow()
0048     , mCalendarView(new CalendarView(this))
0049 {
0050     KOCore::self()->addXMLGUIClient(this, this);
0051     //  setMinimumSize(600,400);  // make sure we don't get resized too small...
0052 
0053     auto mainWidget = new QWidget(this);
0054     auto mainWidgetLayout = new QVBoxLayout(mainWidget);
0055     mainWidgetLayout->setContentsMargins({});
0056     mainWidgetLayout->setSpacing(0);
0057     if (PimCommon::NeedUpdateVersionUtils::checkVersion()) {
0058         const auto status = PimCommon::NeedUpdateVersionUtils::obsoleteVersionStatus(KAboutData::applicationData().version(), QDate::currentDate());
0059         if (status != PimCommon::NeedUpdateVersionUtils::ObsoleteVersion::NotObsoleteYet) {
0060             auto needUpdateVersionWidget = new PimCommon::NeedUpdateVersionWidget(this);
0061             mainWidgetLayout->addWidget(needUpdateVersionWidget);
0062             needUpdateVersionWidget->setObsoleteVersion(status);
0063         }
0064     }
0065     mainWidgetLayout->addWidget(mCalendarView);
0066 
0067     mCalendarView->setObjectName(QLatin1StringView("KOrganizer::CalendarView"));
0068     setCentralWidget(mainWidget);
0069 
0070     mActionManager = new ActionManager(this, mCalendarView, this, this, false, menuBar(), toolBar());
0071     (void)new KOrganizerIfaceImpl(mActionManager, this, QStringLiteral("IfaceImpl"));
0072 
0073 #ifdef WITH_KUSERFEEDBACK
0074     auto userFeedBackNotificationPopup = new KUserFeedback::NotificationPopup(this);
0075     userFeedBackNotificationPopup->setFeedbackProvider(UserFeedBackManager::self()->userFeedbackProvider());
0076 #endif
0077 }
0078 
0079 KOrganizer::~KOrganizer()
0080 {
0081     delete mActionManager;
0082 
0083     KOCore::self()->removeXMLGUIClient(this);
0084 }
0085 
0086 void KOrganizer::init(bool document)
0087 {
0088     setHasDocument(document);
0089 
0090     // Create calendar object, which manages all calendar information associated
0091     // with this calendar view window.
0092     mActionManager->createCalendarAkonadi();
0093 
0094     mActionManager->init();
0095 
0096     KOrganizerPluginInterface::self()->setActionCollection(actionCollection());
0097     KOrganizerPluginInterface::self()->initializePlugins();
0098     initActions();
0099     readSettings();
0100 
0101     QStatusBar *bar = statusBar();
0102 
0103     bar->addWidget(new QLabel(this));
0104 
0105     auto progressBar = new KPIM::ProgressStatusBarWidget(statusBar(), this);
0106 
0107     bar->addPermanentWidget(progressBar->littleProgress());
0108 
0109     connect(mActionManager->view(), &CalendarView::statusMessage, this, &KOrganizer::showStatusMessage);
0110 
0111     setStandardToolBarMenuEnabled(true);
0112     setTitle();
0113 }
0114 
0115 #if 0
0116 void KOrganizer::initializePluginActions()
0117 {
0118 #if 0
0119     if (mXmlGuiClient->factory()) {
0120         QHashIterator<PimCommon::ActionType::Type, QList<QAction *> > localActionsType(
0121             mPluginInterface->actionsType());
0122         while (localActionsType.hasNext()) {
0123             localActionsType.next();
0124             QList<QAction *> lst = localActionsType.value();
0125             if (!lst.isEmpty()) {
0126                 const QString actionlistname = QStringLiteral("korganizer")
0127                                                + PimCommon::PluginInterface::actionXmlExtension(
0128                     localActionsType.key());
0129                 mXmlGuiClient->unplugActionList(actionlistname);
0130                 mXmlGuiClient->plugActionList(actionlistname, lst);
0131             }
0132         }
0133     }
0134 #else
0135     qCDebug(KORGANIZER_LOG) << " Plugins not implemented yet";
0136 #endif
0137 }
0138 
0139 #endif
0140 
0141 void KOrganizer::newMainWindow(const QUrl &url)
0142 {
0143     auto korg = new KOrganizer();
0144     if (url.isValid() || url.isEmpty()) {
0145         korg->init(true);
0146         if (mActionManager->importURL(url, false) || url.isEmpty()) {
0147             korg->show();
0148         } else {
0149             delete korg;
0150         }
0151     } else {
0152         korg->init(false);
0153         korg->show();
0154     }
0155 }
0156 
0157 void KOrganizer::readSettings()
0158 {
0159     // read settings from the KConfig, supplying reasonable
0160     // defaults where none are to be found
0161 
0162     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0163     mActionManager->readSettings();
0164     config->sync();
0165 }
0166 
0167 void KOrganizer::writeSettings()
0168 {
0169     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0170     mActionManager->writeSettings();
0171     config->sync();
0172 }
0173 
0174 void KOrganizer::initActions()
0175 {
0176     setStandardToolBarMenuEnabled(true);
0177     createStandardStatusBarAction();
0178 
0179     KStandardAction::keyBindings(this, &KOrganizer::slotEditKeys, actionCollection());
0180     KStandardAction::configureToolbars(this, &KOrganizer::configureToolbars, actionCollection());
0181     KStandardAction::quit(this, &KOrganizer::close, actionCollection());
0182 
0183     setXMLFile(QStringLiteral("korganizerui.rc"), true);
0184     createGUI(nullptr);
0185 
0186     setAutoSaveSettings();
0187 }
0188 
0189 void KOrganizer::slotEditKeys()
0190 {
0191     KShortcutsDialog::showDialog(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed);
0192 }
0193 
0194 bool KOrganizer::queryClose()
0195 {
0196     bool close = mActionManager->queryClose();
0197 
0198     // Write configuration. I don't know if it really makes sense doing it this
0199     // way, when having opened multiple calendars in different CalendarViews.
0200     if (close) {
0201         writeSettings();
0202     }
0203 
0204     return close;
0205 }
0206 
0207 void KOrganizer::showStatusMessage(const QString &message)
0208 {
0209     statusBar()->showMessage(message, 2000);
0210 }
0211 
0212 bool KOrganizer::openURL(const QUrl &url, bool merge)
0213 {
0214     return mActionManager->importURL(url, merge);
0215 }
0216 
0217 bool KOrganizer::saveURL()
0218 {
0219     return mActionManager->saveURL();
0220 }
0221 
0222 bool KOrganizer::saveAsURL(const QUrl &kurl)
0223 {
0224     return mActionManager->saveAsURL(kurl);
0225 }
0226 
0227 QUrl KOrganizer::getCurrentURL() const
0228 {
0229     return mActionManager->url();
0230 }
0231 
0232 KXMLGUIFactory *KOrganizer::mainGuiFactory()
0233 {
0234     return factory();
0235 }
0236 
0237 KXMLGUIClient *KOrganizer::mainGuiClient()
0238 {
0239     return this;
0240 }
0241 
0242 QWidget *KOrganizer::topLevelWidget()
0243 {
0244     return this;
0245 }
0246 
0247 void KOrganizer::saveProperties(KConfigGroup &config)
0248 {
0249     mActionManager->saveProperties(config);
0250 }
0251 
0252 void KOrganizer::readProperties(const KConfigGroup &config)
0253 {
0254     mActionManager->readProperties(config);
0255 }
0256 
0257 KOrg::CalendarViewBase *KOrganizer::view() const
0258 {
0259     return mActionManager->view();
0260 }
0261 
0262 ActionManager *KOrganizer::actionManager()
0263 {
0264     return mActionManager;
0265 }
0266 
0267 KActionCollection *KOrganizer::getActionCollection() const
0268 {
0269     return actionCollection();
0270 }
0271 
0272 void KOrganizer::setTitle()
0273 {
0274     QString title;
0275     if (hasDocument()) {
0276         const QUrl url = mActionManager->url();
0277 
0278         if (!url.isEmpty()) {
0279             if (url.isLocalFile()) {
0280                 title = url.fileName();
0281             } else {
0282                 title = url.toDisplayString();
0283             }
0284         } else {
0285             title = i18n("New Calendar");
0286         }
0287 
0288         if (mCalendarView->isReadOnly()) {
0289             title += QLatin1StringView(" [") + i18nc("the calendar is read-only", "read-only") + QLatin1Char(']');
0290         }
0291     } else {
0292         title = i18n("Calendar");
0293     }
0294     if (mCalendarView->isFiltered()) {
0295         title += QLatin1StringView(" - <") + mCalendarView->currentFilterName() + QLatin1StringView("> ");
0296     }
0297 
0298     setCaption(title, false);
0299 }
0300 
0301 #include "moc_korganizer.cpp"