1 /*
2   This file is part of KOrganizer.
3 
4   SPDX-FileCopyrightText: 1997, 1998, 1999 Preston Brown <preston.brown@yale.edu>
5   SPDX-FileCopyrightText: Fester Zigterman <F.J.F.ZigtermanRustenburg@student.utwente.nl>
6   SPDX-FileCopyrightText: Ian Dawes <iadawes@globalserve.net>
7   SPDX-FileCopyrightText: Laszlo Boloni <boloni@cs.purdue.edu>
8 
9   SPDX-FileCopyrightText: 2000-2003 Cornelius Schumacher <schumacher@kde.org>
10   SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
11 
12   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
13 */
14 
15 #include "korganizer.h"
16 #include "actionmanager.h"
17 #include "calendarview.h"
18 #include "impl/korganizerifaceimpl.h"
19 #include "kocore.h"
20 #include "koglobals.h"
21 #include "plugininterface/korganizerplugininterface.h"
22 
23 #include <Libkdepim/ProgressStatusBarWidget>
24 #include <Libkdepim/StatusbarProgressWidget>
25 
26 #include "korganizer_debug.h"
27 #include <KActionCollection>
28 #include <KSharedConfig>
29 #include <KShortcutsDialog>
30 #include <KStandardAction>
31 #include <KToolBar>
32 #include <QLabel>
33 #include <QStatusBar>
34 #ifdef WITH_KUSERFEEDBACK
35 #include "userfeedback/userfeedbackmanager.h"
36 #include <KUserFeedback/NotificationPopup>
37 #include <KUserFeedback/Provider>
38 #endif
39 
KOrganizer()40 KOrganizer::KOrganizer()
41     : KParts::MainWindow()
42     , KOrg::MainWindow()
43     , mCalendarView(new CalendarView(this))
44 {
45     // Set this to be the group leader for all subdialogs - this means
46     // modal subdialogs will only affect this dialog, not the other windows
47     setAttribute(Qt::WA_GroupLeader);
48 
49     KOCore::self()->addXMLGUIClient(this, this);
50     //  setMinimumSize(600,400);  // make sure we don't get resized too small...
51 
52     mCalendarView->setObjectName(QStringLiteral("KOrganizer::CalendarView"));
53     setCentralWidget(mCalendarView);
54 
55     mActionManager = new ActionManager(this, mCalendarView, this, this, false, menuBar(), toolBar());
56     (void)new KOrganizerIfaceImpl(mActionManager, this, QStringLiteral("IfaceImpl"));
57 
58 #ifdef WITH_KUSERFEEDBACK
59     auto userFeedBackNotificationPopup = new KUserFeedback::NotificationPopup(this);
60     userFeedBackNotificationPopup->setFeedbackProvider(UserFeedBackManager::self()->userFeedbackProvider());
61 #endif
62 }
63 
~KOrganizer()64 KOrganizer::~KOrganizer()
65 {
66     delete mActionManager;
67 
68     KOCore::self()->removeXMLGUIClient(this);
69 }
70 
init(bool document)71 void KOrganizer::init(bool document)
72 {
73     setHasDocument(document);
74 
75     // Create calendar object, which manages all calendar information associated
76     // with this calendar view window.
77     mActionManager->createCalendarAkonadi();
78 
79     mActionManager->init();
80 
81     KOrganizerPluginInterface::self()->setActionCollection(actionCollection());
82     KOrganizerPluginInterface::self()->initializePlugins();
83     initActions();
84     readSettings();
85 
86     QStatusBar *bar = statusBar();
87 
88     bar->addWidget(new QLabel(this));
89 
90     auto progressBar = new KPIM::ProgressStatusBarWidget(statusBar(), this);
91 
92     bar->addPermanentWidget(progressBar->littleProgress());
93 
94     connect(mActionManager->view(), &CalendarView::statusMessage, this, &KOrganizer::showStatusMessage);
95 
96     setStandardToolBarMenuEnabled(true);
97     setTitle();
98 }
99 
100 #if 0
101 void KOrganizer::initializePluginActions()
102 {
103 #if 0
104     if (mXmlGuiClient->factory()) {
105         QHashIterator<PimCommon::ActionType::Type, QList<QAction *> > localActionsType(
106             mPluginInterface->actionsType());
107         while (localActionsType.hasNext()) {
108             localActionsType.next();
109             QList<QAction *> lst = localActionsType.value();
110             if (!lst.isEmpty()) {
111                 const QString actionlistname = QStringLiteral("korganizer")
112                                                + PimCommon::PluginInterface::actionXmlExtension(
113                     localActionsType.key());
114                 mXmlGuiClient->unplugActionList(actionlistname);
115                 mXmlGuiClient->plugActionList(actionlistname, lst);
116             }
117         }
118     }
119 #else
120     qCDebug(KORGANIZER_LOG) << " Plugins not implemented yet";
121 #endif
122 }
123 
124 #endif
125 
newMainWindow(const QUrl & url)126 void KOrganizer::newMainWindow(const QUrl &url)
127 {
128     auto korg = new KOrganizer();
129     if (url.isValid() || url.isEmpty()) {
130         korg->init(true);
131         if (mActionManager->importURL(url, false) || url.isEmpty()) {
132             korg->show();
133         } else {
134             delete korg;
135         }
136     } else {
137         korg->init(false);
138         korg->show();
139     }
140 }
141 
readSettings()142 void KOrganizer::readSettings()
143 {
144     // read settings from the KConfig, supplying reasonable
145     // defaults where none are to be found
146 
147     KSharedConfig::Ptr config = KSharedConfig::openConfig();
148     mActionManager->readSettings();
149     config->sync();
150 }
151 
writeSettings()152 void KOrganizer::writeSettings()
153 {
154     KSharedConfig::Ptr config = KSharedConfig::openConfig();
155     mActionManager->writeSettings();
156     config->sync();
157 }
158 
initActions()159 void KOrganizer::initActions()
160 {
161     setStandardToolBarMenuEnabled(true);
162     createStandardStatusBarAction();
163 
164     KStandardAction::keyBindings(this, &KOrganizer::slotEditKeys, actionCollection());
165     KStandardAction::configureToolbars(this, &KOrganizer::configureToolbars, actionCollection());
166     KStandardAction::quit(this, &KOrganizer::close, actionCollection());
167 
168     setXMLFile(QStringLiteral("korganizerui.rc"), true);
169     createGUI(nullptr);
170 
171     setAutoSaveSettings();
172 }
173 
slotEditKeys()174 void KOrganizer::slotEditKeys()
175 {
176     KShortcutsDialog::showDialog(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed);
177 }
178 
queryClose()179 bool KOrganizer::queryClose()
180 {
181     bool close = mActionManager->queryClose();
182 
183     // Write configuration. I don't know if it really makes sense doing it this
184     // way, when having opened multiple calendars in different CalendarViews.
185     if (close) {
186         writeSettings();
187     }
188 
189     return close;
190 }
191 
showStatusMessage(const QString & message)192 void KOrganizer::showStatusMessage(const QString &message)
193 {
194     statusBar()->showMessage(message, 2000);
195 }
196 
openURL(const QUrl & url,bool merge)197 bool KOrganizer::openURL(const QUrl &url, bool merge)
198 {
199     return mActionManager->importURL(url, merge);
200 }
201 
saveURL()202 bool KOrganizer::saveURL()
203 {
204     return mActionManager->saveURL();
205 }
206 
saveAsURL(const QUrl & kurl)207 bool KOrganizer::saveAsURL(const QUrl &kurl)
208 {
209     return mActionManager->saveAsURL(kurl);
210 }
211 
getCurrentURL() const212 QUrl KOrganizer::getCurrentURL() const
213 {
214     return mActionManager->url();
215 }
216 
mainGuiFactory()217 KXMLGUIFactory *KOrganizer::mainGuiFactory()
218 {
219     return factory();
220 }
221 
mainGuiClient()222 KXMLGUIClient *KOrganizer::mainGuiClient()
223 {
224     return this;
225 }
226 
topLevelWidget()227 QWidget *KOrganizer::topLevelWidget()
228 {
229     return this;
230 }
231 
saveProperties(KConfigGroup & config)232 void KOrganizer::saveProperties(KConfigGroup &config)
233 {
234     mActionManager->saveProperties(config);
235 }
236 
readProperties(const KConfigGroup & config)237 void KOrganizer::readProperties(const KConfigGroup &config)
238 {
239     mActionManager->readProperties(config);
240 }
241 
view() const242 KOrg::CalendarViewBase *KOrganizer::view() const
243 {
244     return mActionManager->view();
245 }
246 
actionManager()247 ActionManager *KOrganizer::actionManager()
248 {
249     return mActionManager;
250 }
251 
getActionCollection() const252 KActionCollection *KOrganizer::getActionCollection() const
253 {
254     return actionCollection();
255 }
256 
setTitle()257 void KOrganizer::setTitle()
258 {
259     QString title;
260     if (hasDocument()) {
261         const QUrl url = mActionManager->url();
262 
263         if (!url.isEmpty()) {
264             if (url.isLocalFile()) {
265                 title = url.fileName();
266             } else {
267                 title = url.toDisplayString();
268             }
269         } else {
270             title = i18n("New Calendar");
271         }
272 
273         if (mCalendarView->isReadOnly()) {
274             title += QLatin1String(" [") + i18nc("the calendar is read-only", "read-only") + QLatin1Char(']');
275         }
276     } else {
277         title = i18n("Calendar");
278     }
279     if (mCalendarView->isFiltered()) {
280         title += QLatin1String(" - <") + mCalendarView->currentFilterName() + QLatin1String("> ");
281     }
282 
283     setCaption(title, false);
284 }
285