Show conference offset in conference dialog and prepare display offset.
[debian/confclerk.git] / src / gui / conferenceeditor.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011-2017 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
54     // it's OK to emit selection signals here
55     // because they are not yet connected to anybody
56     int active_id = Conference::activeConference();
57     if (active_id > 0) {
58         emit wantCurrent(model->indexFromId(active_id), QItemSelectionModel::SelectCurrent);
59     } else {
60         itemSelected(QModelIndex(), QModelIndex());
61     }
62 }
63
64 void ConferenceEditor::conferenceRemoved()
65 {
66     if (model->rowCount() > 0) {
67         emit wantCurrent(model->index(0, 0), QItemSelectionModel::SelectCurrent);
68     } else {
69         itemSelected(QModelIndex(), QModelIndex());
70     }
71 }
72
73 void ConferenceEditor::itemSelected(const QModelIndex& current, const QModelIndex& previous)
74 {
75     // TODO: fill all required fields
76     Q_UNUSED(previous);
77     if (!current.isValid()) {
78         selected_id = -1;
79
80         emit noneConferenceSelected();
81
82         conferenceInfo->setCurrentIndex(1);
83         removeBtn->hide();
84     } else {
85         const Conference& conf = model->conferenceFromIndex(current);
86         selected_id = conf.id();
87
88         emit haveConferenceSelected(selected_id);
89
90         conferenceTitle->setText(conf.title());
91         conferenceSubtitle->setText(conf.subtitle());
92         conferenceWhere->setText(conf.city() + (!conf.venue().isEmpty() ? ", " + conf.venue() : ""));
93         conferenceWhen->setText(
94                 conf.start().toString("yyyy-MM-dd")
95                 + " - " +
96                 conf.end().toString("yyyy-MM-dd"));
97         if (conf.hasUtcOffset()) {
98             conferenceUtcOffset->setText(QString::number(conf.utcOffset()) + " min");
99         } else {
100             conferenceUtcOffset->setText("N/A");
101         }
102         conferenceInfo->setCurrentIndex(0);
103         removeBtn->show();
104     }
105 }
106
107 void ConferenceEditor::addClicked()
108 {
109     UrlInputDialog url_input(this);
110     switch (url_input.exec()) {
111     case UrlInputDialog::HaveUrl: emit haveConferenceUrl(url_input.url(), 0); break;
112     case UrlInputDialog::HaveFile: emit haveConferenceFile(url_input.url(), 0); break;
113     case UrlInputDialog::Cancel: return;
114     }
115 }
116
117 void ConferenceEditor::removeClicked()
118 {
119     if (selected_id < 0) {
120         // TODO: disable it when none is selected
121         return;
122     }
123
124     QMessageBox::StandardButton answer = 
125         QMessageBox::question(0
126             , "Deletion confirmation"
127             , QString("Really delete the %1 conference").arg(Conference::getById(selected_id).title())
128             , QMessageBox::Yes | QMessageBox::No
129             , QMessageBox::No);
130
131     if (answer == QMessageBox::Yes) {
132         emit removeConferenceRequested(selected_id);
133     }
134 }
135
136 void ConferenceEditor::changeUrlClicked()
137 {
138     if (selected_id < 0) return;
139     const Conference& selectedConf = Conference::getById(selected_id);
140
141     bool ok;
142     QString url = QInputDialog::getText(this, "URL Input", "Enter schedule URL", QLineEdit::Normal, selectedConf.url(), &ok);
143
144     if (ok) {
145         emit changeUrlRequested(selected_id, url);
146     }
147 }
148
149 void ConferenceEditor::refreshClicked()
150 {
151     if (selected_id <= 0) return;
152     const Conference& selectedConf = Conference::getById(selected_id);
153     QString url = selectedConf.url();
154
155     if (url.isEmpty()) {
156         static const QString format("Schedule URL for %1 is not set. Enter the schedule URL:");
157         bool ok;
158         url = QInputDialog::getText(this, "URL Input", format.arg(selectedConf.title()), QLineEdit::Normal, QString(), &ok);
159         if (!ok) return;
160         // first save it, to remain if fetch fails
161         emit changeUrlRequested(selected_id, url);
162     }
163     // fetch
164     importStarted(); // just to show the progress bar
165     emit haveConferenceUrl(url, selected_id);
166 }
167
168 void ConferenceEditor::importStarted()
169 {
170     addBtn->hide();
171     removeBtn->hide();
172     buttons->layout()->removeItem(buttonsSpacer);
173     progressBar->setValue(0);
174     progressBar->show();
175
176     QApplication::processEvents();
177 }
178
179 void ConferenceEditor::showParsingProgress(int progress)
180 {
181     progressBar->setValue(progress);
182
183     QApplication::processEvents();
184 }
185
186 void ConferenceEditor::importFinished(int conferenceId) {
187     addBtn->show();
188     // removeItem should be shown later, but it takes some time,
189     // and not looks good
190     // anyway it will be shown a bit later
191     removeBtn->show();
192     buttons->layout()->addItem(buttonsSpacer);
193     progressBar->hide();
194
195     QApplication::processEvents();
196
197     QModelIndex item = model->indexFromId(conferenceId);
198     if (item.isValid())
199         emit wantCurrent(item, QItemSelectionModel::SelectCurrent);
200     else
201         itemSelected(QModelIndex(), QModelIndex());
202 }
203