1 /*
2   This file is part of KOrganizer.
3 
4   SPDX-FileCopyrightText: 2000, 2001, 2003 Cornelius Schumacher <schumacher@kde.org>
5   SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6   SPDX-FileCopyrightText: 2005 Rafal Rzepecki <divide@users.sourceforge.net>
7   SPDX-FileCopyrightText: 2008 Thomas Thrainer <tom_t@gmx.at>
8 
9   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
10 */
11 
12 #include "kotodoview.h"
13 #include "koeventpopupmenu.h"
14 #include "prefs/koprefs.h"
15 
16 #include <CalendarSupport/CalPrinter>
17 
18 #include <Akonadi/EntityTreeModel>
19 
20 #include <QVBoxLayout>
21 
KOTodoView(bool sidebarView,QWidget * parent)22 KOTodoView::KOTodoView(bool sidebarView, QWidget *parent)
23     : BaseView(parent)
24 {
25     auto eventPopup = new KOEventPopupMenu(calendar(), this);
26     mView = new EventViews::TodoView(KOPrefs::instance()->eventViewsPreferences(), sidebarView, parent);
27     auto layout = new QVBoxLayout(this);
28     layout->addWidget(mView);
29     connect(mView, &EventViews::TodoView::printTodo, this, [this]() {
30         printTodo();
31     });
32     connect(mView, &EventViews::TodoView::printPreviewTodo, this, &KOTodoView::printPreviewTodo);
33     connect(mView, &EventViews::TodoView::purgeCompletedSignal, this, &KOTodoView::purgeCompletedSignal);
34 
35     connect(mView, &EventViews::EventView::incidenceSelected, this, &BaseView::incidenceSelected);
36 
37     connect(mView, &EventViews::EventView::showIncidenceSignal, this, &BaseView::showIncidenceSignal);
38 
39     connect(mView, &EventViews::EventView::editIncidenceSignal, this, &BaseView::editIncidenceSignal);
40 
41     connect(mView, &EventViews::EventView::deleteIncidenceSignal, this, &BaseView::deleteIncidenceSignal);
42 
43     connect(mView, &EventViews::EventView::cutIncidenceSignal, this, &BaseView::cutIncidenceSignal);
44 
45     connect(mView, &EventViews::EventView::copyIncidenceSignal, this, &BaseView::copyIncidenceSignal);
46 
47     connect(mView, &EventViews::EventView::pasteIncidenceSignal, this, &BaseView::pasteIncidenceSignal);
48 
49     connect(mView, &EventViews::EventView::toggleAlarmSignal, this, &BaseView::toggleAlarmSignal);
50 
51     connect(mView, &EventViews::EventView::toggleTodoCompletedSignal, this, &BaseView::toggleTodoCompletedSignal);
52 
53     connect(mView, &EventViews::EventView::copyIncidenceToResourceSignal, this, &BaseView::copyIncidenceToResourceSignal);
54 
55     connect(mView, &EventViews::EventView::moveIncidenceToResourceSignal, this, &BaseView::moveIncidenceToResourceSignal);
56 
57     connect(mView, &EventViews::EventView::dissociateOccurrencesSignal, this, &BaseView::dissociateOccurrencesSignal);
58 
59     connect(mView, qOverload<>(&EventViews::TodoView::newEventSignal), this, qOverload<>(&KOTodoView::newEventSignal));
60 
61     connect(mView, qOverload<const QDate &>(&EventViews::TodoView::newEventSignal),
62             this, qOverload<const QDate &>(&KOTodoView::newEventSignal));
63 
64     connect(mView, qOverload<const QDateTime &>(&EventViews::TodoView::newEventSignal),
65             this, qOverload<const QDateTime &>(&KOTodoView::newEventSignal));
66 
67     connect(mView, qOverload<const QDateTime &, const QDateTime &>(&EventViews::TodoView::newEventSignal),
68             this, qOverload<const QDateTime &, const QDateTime &>(&KOTodoView::newEventSignal));
69 
70     connect(mView, &EventViews::EventView::newTodoSignal, this, &BaseView::newTodoSignal);
71 
72     connect(mView, &EventViews::EventView::newSubTodoSignal, this, &BaseView::newSubTodoSignal);
73 
74     connect(mView, &EventViews::TodoView::fullViewChanged, this, &KOTodoView::fullViewChanged);
75 
76     connect(mView, &EventViews::TodoView::unSubTodoSignal, this, &KOTodoView::unSubTodoSignal);
77     connect(mView, &EventViews::TodoView::unAllSubTodoSignal, this, &KOTodoView::unAllSubTodoSignal);
78 
79     connect(mView,
80             static_cast<void (EventViews::TodoView::*)(const Akonadi::Item &)>(&EventViews::TodoView ::createEvent),
81             eventPopup,
82             static_cast<void (KOEventPopupMenu::*)(const Akonadi::Item &)>(&KOEventPopupMenu::createEvent));
83     connect(mView,
84             static_cast<void (EventViews::TodoView::*)(const Akonadi::Item &)>(&EventViews::TodoView ::createNote),
85             eventPopup,
86             static_cast<void (KOEventPopupMenu::*)(const Akonadi::Item &)>(&KOEventPopupMenu::createNote));
87 }
88 
~KOTodoView()89 KOTodoView::~KOTodoView()
90 {
91 }
92 
setCalendar(const Akonadi::ETMCalendar::Ptr & calendar)93 void KOTodoView::setCalendar(const Akonadi::ETMCalendar::Ptr &calendar)
94 {
95     BaseView::setCalendar(calendar);
96     mView->setCalendar(calendar);
97 }
98 
selectedIncidences()99 Akonadi::Item::List KOTodoView::selectedIncidences()
100 {
101     return mView->selectedIncidences();
102 }
103 
selectedIncidenceDates()104 KCalendarCore::DateList KOTodoView::selectedIncidenceDates()
105 {
106     return KCalendarCore::DateList();
107 }
108 
setIncidenceChanger(Akonadi::IncidenceChanger * changer)109 void KOTodoView::setIncidenceChanger(Akonadi::IncidenceChanger *changer)
110 {
111     BaseView::setIncidenceChanger(changer);
112     mView->setIncidenceChanger(changer);
113 }
114 
showDates(const QDate & start,const QDate & end,const QDate &)115 void KOTodoView::showDates(const QDate &start, const QDate &end, const QDate &)
116 {
117     // There is nothing to do here for the Todo View
118     Q_UNUSED(start)
119     Q_UNUSED(end)
120 }
121 
showIncidences(const Akonadi::Item::List & incidenceList,const QDate & date)122 void KOTodoView::showIncidences(const Akonadi::Item::List &incidenceList, const QDate &date)
123 {
124     Q_UNUSED(incidenceList)
125     Q_UNUSED(date)
126 }
127 
updateView()128 void KOTodoView::updateView()
129 {
130     // View is always updated, it's connected to ETM.
131 }
132 
changeIncidenceDisplay(const Akonadi::Item &,Akonadi::IncidenceChanger::ChangeType)133 void KOTodoView::changeIncidenceDisplay(const Akonadi::Item &, Akonadi::IncidenceChanger::ChangeType)
134 {
135     // Don't do anything, model is connected to ETM, it's up to date
136 }
137 
updateConfig()138 void KOTodoView::updateConfig()
139 {
140     mView->updateConfig();
141 }
142 
clearSelection()143 void KOTodoView::clearSelection()
144 {
145     mView->clearSelection();
146 }
147 
printTodo()148 void KOTodoView::printTodo()
149 {
150     printTodo(false);
151 }
152 
printPreviewTodo()153 void KOTodoView::printPreviewTodo()
154 {
155     printTodo(true);
156 }
157 
printTodo(bool preview)158 void KOTodoView::printTodo(bool preview)
159 {
160     const Akonadi::Item::List selectedItems = mView->selectedIncidences();
161     if (selectedItems.count() != 1) {
162         return;
163     }
164 
165     Akonadi::Item todoItem = selectedItems.first();
166     KCalendarCore::Todo::Ptr todo = CalendarSupport::todo(todoItem);
167     Q_ASSERT(todo);
168 
169     CalendarSupport::CalPrinter printer(this, calendar(), true);
170     connect(this, &KOTodoView::configChanged, &printer, &CalendarSupport::CalPrinter::updateConfig);
171 
172     KCalendarCore::Incidence::List selectedIncidences;
173     selectedIncidences.append(todo);
174 
175     QDate todoDate;
176     if (todo->hasStartDate()) {
177         todoDate = todo->dtStart().date();
178     } else {
179         todoDate = todo->dtDue().date();
180     }
181 
182     printer.print(CalendarSupport::CalPrinterBase::Incidence, todoDate, todoDate, selectedIncidences, preview);
183 }
184 
getHighlightMode(bool & highlightEvents,bool & highlightTodos,bool & highlightJournals)185 void KOTodoView::getHighlightMode(bool &highlightEvents, bool &highlightTodos, bool &highlightJournals)
186 {
187     highlightTodos = KOPrefs::instance()->mHighlightTodos;
188     highlightEvents = !highlightTodos;
189     highlightJournals = false;
190 }
191 
saveViewState()192 void KOTodoView::saveViewState()
193 {
194     mView->saveViewState();
195 }
196 
restoreViewState()197 void KOTodoView::restoreViewState()
198 {
199     mView->restoreViewState();
200 }
201 
restoreLayout(KConfig * config,const QString & group,bool minimalDefaults)202 void KOTodoView::restoreLayout(KConfig *config, const QString &group, bool minimalDefaults)
203 {
204     mView->restoreLayout(config, group, minimalDefaults);
205 }
206 
saveLayout(KConfig * config,const QString & group) const207 void KOTodoView::saveLayout(KConfig *config, const QString &group) const
208 {
209     mView->saveLayout(config, group);
210 }
211 
usesFullWindow()212 bool KOTodoView::usesFullWindow()
213 {
214     return mView->usesFullWindow();
215 }
216 
printType() const217 CalendarSupport::CalPrinterBase::PrintType KOTodoView::printType() const
218 {
219     return CalendarSupport::CalPrinterBase::Todolist;
220 }
221