1 | /* |
---|
2 | * Copyright (C) 2010 Ixonos Plc. |
---|
3 | * Copyright (C) 2011-2021 Philipp Spitzer, gregor herrmann, Stefan Stahl |
---|
4 | * |
---|
5 | * This file is part of ConfClerk. |
---|
6 | * |
---|
7 | * ConfClerk is free software: you can redistribute it and/or modify it |
---|
8 | * under the terms of the GNU General Public License as published by the Free |
---|
9 | * Software Foundation, either version 2 of the License, or (at your option) |
---|
10 | * any later version. |
---|
11 | * |
---|
12 | * ConfClerk is distributed in the hope that it will be useful, but |
---|
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
15 | * more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License along with |
---|
18 | * ConfClerk. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | */ |
---|
20 | #include "conferenceeditor.h" |
---|
21 | |
---|
22 | #include "conferencemodel.h" |
---|
23 | #include "urlinputdialog.h" |
---|
24 | #include "errormessage.h" |
---|
25 | |
---|
26 | #include <QInputDialog> |
---|
27 | #include <QItemSelectionModel> |
---|
28 | #include <QFileDialog> |
---|
29 | #include <QMessageBox> |
---|
30 | |
---|
31 | ConferenceEditor::ConferenceEditor(ConferenceModel* model, QWidget* parent) |
---|
32 | : QDialog(parent) |
---|
33 | , model(model) |
---|
34 | , selected_id(-1) |
---|
35 | { |
---|
36 | setupUi(this); |
---|
37 | progressBar->hide(); |
---|
38 | |
---|
39 | confView->setModel(model); |
---|
40 | |
---|
41 | QItemSelectionModel* confViewSelection = new QItemSelectionModel(model, this); |
---|
42 | confView->setSelectionModel(confViewSelection); |
---|
43 | |
---|
44 | connect(confViewSelection, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), |
---|
45 | SLOT(itemSelected(const QModelIndex&, const QModelIndex&))); |
---|
46 | connect(this, SIGNAL(wantCurrent(const QModelIndex&, QItemSelectionModel::SelectionFlags)), |
---|
47 | confViewSelection, SLOT(setCurrentIndex(const QModelIndex&, QItemSelectionModel::SelectionFlags))); |
---|
48 | connect(addBtn, SIGNAL(clicked()), SLOT(addClicked())); |
---|
49 | connect(removeBtn, SIGNAL(clicked()), SLOT(removeClicked())); |
---|
50 | connect(changeUrl, SIGNAL(clicked()), SLOT(changeUrlClicked())); |
---|
51 | connect(refreshBtn, SIGNAL(clicked()), SLOT(refreshClicked())); |
---|
52 | connect(buttonBox, SIGNAL(rejected()), SLOT(close())); |
---|
53 | connect(conferenceDtsHours, SIGNAL(valueChanged(int)), SLOT(dtsChanged())); |
---|
54 | connect(conferenceDtsMinutes, SIGNAL(valueChanged(int)), SLOT(dtsChanged())); |
---|
55 | |
---|
56 | // it's OK to emit selection signals here |
---|
57 | // because they are not yet connected to anybody |
---|
58 | int active_id = Conference::activeConference(); |
---|
59 | if (active_id > 0) { |
---|
60 | emit wantCurrent(model->indexFromId(active_id), QItemSelectionModel::SelectCurrent); |
---|
61 | } else { |
---|
62 | itemSelected(QModelIndex(), QModelIndex()); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | void ConferenceEditor::conferenceRemoved() |
---|
67 | { |
---|
68 | if (model->rowCount() > 0) { |
---|
69 | emit wantCurrent(model->index(0, 0), QItemSelectionModel::SelectCurrent); |
---|
70 | } else { |
---|
71 | itemSelected(QModelIndex(), QModelIndex()); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | void ConferenceEditor::itemSelected(const QModelIndex& current, const QModelIndex& previous) |
---|
76 | { |
---|
77 | // TODO: fill all required fields |
---|
78 | Q_UNUSED(previous); |
---|
79 | if (!current.isValid()) { |
---|
80 | selected_id = -1; |
---|
81 | |
---|
82 | emit noneConferenceSelected(); |
---|
83 | |
---|
84 | conferenceInfo->setCurrentIndex(1); |
---|
85 | removeBtn->hide(); |
---|
86 | } else { |
---|
87 | const Conference& conf = model->conferenceFromIndex(current); |
---|
88 | selected_id = conf.id(); |
---|
89 | |
---|
90 | emit haveConferenceSelected(selected_id); |
---|
91 | |
---|
92 | conferenceTitle->setText(conf.title()); |
---|
93 | conferenceSubtitle->setText(conf.subtitle()); |
---|
94 | QString where = conf.city(); |
---|
95 | if (!conf.city().isEmpty() && !conf.venue().isEmpty()) where += ", "; |
---|
96 | where += conf.venue(); |
---|
97 | conferenceWhere->setText(where); |
---|
98 | conferenceWhen->setText( |
---|
99 | conf.start().toString("yyyy-MM-dd") |
---|
100 | + " - " + |
---|
101 | conf.end().toString("yyyy-MM-dd")); |
---|
102 | if (conf.hasUtcOffset()) { |
---|
103 | conferenceUtcOffset->setText(QString::number(conf.utcOffset()) + " min"); |
---|
104 | } else { |
---|
105 | conferenceUtcOffset->setText("N/A"); |
---|
106 | } |
---|
107 | int dts = conf.displayTimeShift(); |
---|
108 | conferenceDtsHours->setValue(dts / 60); |
---|
109 | conferenceDtsMinutes->setValue(abs(dts) % 60); |
---|
110 | conferenceInfo->setCurrentIndex(0); |
---|
111 | removeBtn->show(); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | void ConferenceEditor::addClicked() |
---|
116 | { |
---|
117 | UrlInputDialog url_input(this); |
---|
118 | switch (url_input.exec()) { |
---|
119 | case UrlInputDialog::HaveUrl: emit haveConferenceUrl(url_input.url(), 0); break; |
---|
120 | case UrlInputDialog::HaveFile: emit haveConferenceFile(url_input.url(), 0); break; |
---|
121 | case UrlInputDialog::Cancel: return; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | void ConferenceEditor::removeClicked() |
---|
126 | { |
---|
127 | if (selected_id < 0) { |
---|
128 | // TODO: disable it when none is selected |
---|
129 | return; |
---|
130 | } |
---|
131 | |
---|
132 | QMessageBox::StandardButton answer = |
---|
133 | QMessageBox::question(0 |
---|
134 | , "Deletion confirmation" |
---|
135 | , QString("Really delete the %1 conference").arg(Conference::getById(selected_id).title()) |
---|
136 | , QMessageBox::Yes | QMessageBox::No |
---|
137 | , QMessageBox::No); |
---|
138 | |
---|
139 | if (answer == QMessageBox::Yes) { |
---|
140 | emit removeConferenceRequested(selected_id); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | void ConferenceEditor::changeUrlClicked() |
---|
145 | { |
---|
146 | if (selected_id < 0) return; |
---|
147 | const Conference& selectedConf = Conference::getById(selected_id); |
---|
148 | |
---|
149 | bool ok; |
---|
150 | QString url = QInputDialog::getText(this, "URL Input", "Enter schedule URL", QLineEdit::Normal, selectedConf.url(), &ok); |
---|
151 | |
---|
152 | if (ok) { |
---|
153 | emit changeUrlRequested(selected_id, url); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | void ConferenceEditor::refreshClicked() |
---|
158 | { |
---|
159 | if (selected_id <= 0) return; |
---|
160 | const Conference& selectedConf = Conference::getById(selected_id); |
---|
161 | QString url = selectedConf.url(); |
---|
162 | |
---|
163 | if (url.isEmpty()) { |
---|
164 | static const QString format("Schedule URL for %1 is not set. Enter the schedule URL:"); |
---|
165 | bool ok; |
---|
166 | url = QInputDialog::getText(this, "URL Input", format.arg(selectedConf.title()), QLineEdit::Normal, QString(), &ok); |
---|
167 | if (!ok) return; |
---|
168 | // first save it, to remain if fetch fails |
---|
169 | emit changeUrlRequested(selected_id, url); |
---|
170 | } |
---|
171 | // fetch |
---|
172 | importStarted(); // just to show the progress bar |
---|
173 | emit haveConferenceUrl(url, selected_id); |
---|
174 | } |
---|
175 | |
---|
176 | void ConferenceEditor::dtsChanged() { |
---|
177 | if (selected_id < 0) return; |
---|
178 | Conference& conference = model->conferenceFromIndex(model->indexFromId(selected_id)); |
---|
179 | int minutes = conferenceDtsMinutes->value(); |
---|
180 | if (conferenceDtsHours->value() < 0) minutes *= -1; |
---|
181 | conference.setDisplayTimeShift(conferenceDtsHours->value() * 60 + minutes); |
---|
182 | } |
---|
183 | |
---|
184 | void ConferenceEditor::importStarted() |
---|
185 | { |
---|
186 | addBtn->hide(); |
---|
187 | removeBtn->hide(); |
---|
188 | buttons->layout()->removeItem(buttonsSpacer); |
---|
189 | progressBar->setValue(0); |
---|
190 | progressBar->show(); |
---|
191 | |
---|
192 | QApplication::processEvents(); |
---|
193 | } |
---|
194 | |
---|
195 | void ConferenceEditor::showParsingProgress(int progress) |
---|
196 | { |
---|
197 | progressBar->setValue(progress); |
---|
198 | |
---|
199 | QApplication::processEvents(); |
---|
200 | } |
---|
201 | |
---|
202 | void ConferenceEditor::importFinished(int conferenceId) { |
---|
203 | addBtn->show(); |
---|
204 | // removeItem should be shown later, but it takes some time, |
---|
205 | // and not looks good |
---|
206 | // anyway it will be shown a bit later |
---|
207 | removeBtn->show(); |
---|
208 | buttons->layout()->addItem(buttonsSpacer); |
---|
209 | progressBar->hide(); |
---|
210 | |
---|
211 | QApplication::processEvents(); |
---|
212 | |
---|
213 | QModelIndex item = model->indexFromId(conferenceId); |
---|
214 | if (item.isValid()) |
---|
215 | emit wantCurrent(item, QItemSelectionModel::SelectCurrent); |
---|
216 | else |
---|
217 | itemSelected(QModelIndex(), QModelIndex()); |
---|
218 | } |
---|
219 | |
---|