Add copyright to source.
[toast/confclerk.git] / src / gui / conferenceeditor.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
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 "mapwindow.h"
25 #include "errormessage.h"
26
27 #include <QInputDialog>
28 #include <QItemSelectionModel>
29 #include <QFileDialog>
30 #include <QMessageBox>
31
32 ConferenceEditor::ConferenceEditor(ConferenceModel* model, QWidget* parent)
33 : QDialog(parent)
34 , model(model)
35 , selected_id(-1)
36 {
37     setupUi(this);
38     progressBar->hide();
39
40     confView->setModel(model);
41
42     QItemSelectionModel* confViewSelection = new QItemSelectionModel(model, this);
43     confView->setSelectionModel(confViewSelection);
44
45     connect(confViewSelection, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
46                                 SLOT(itemSelected(const QModelIndex&, const QModelIndex&)));
47     connect(this, SIGNAL(wantCurrent(const QModelIndex&, QItemSelectionModel::SelectionFlags)),
48             confViewSelection, SLOT(setCurrentIndex(const QModelIndex&, QItemSelectionModel::SelectionFlags)));
49     connect(addBtn, SIGNAL(clicked()), SLOT(addClicked()));
50     connect(removeBtn, SIGNAL(clicked()), SLOT(removeClicked()));
51     connect(changeUrl, SIGNAL(clicked()), SLOT(changeUrlClicked()));
52     connect(refreshBtn, SIGNAL(clicked()), SLOT(refreshClicked()));
53     connect(showMapButton, SIGNAL(clicked()), SLOT(conferenceMapClicked()));
54     connect(buttonBox, SIGNAL(rejected()), SLOT(close()));
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         conferenceWhere->setText(conf.city() + ", " + conf.venue());
95         conferenceWhen->setText(
96                 conf.start().toString("dd-MM-yyyy")
97                 + ", " +
98                 conf.end().toString("dd-MM-yyyy"));
99
100         QString map = conf.map();
101         if (map.isEmpty()) {
102             showMapButton->hide();
103         } else {
104             showMapButton->show();
105         }
106
107         conferenceInfo->setCurrentIndex(0);
108         removeBtn->show();
109     }
110 }
111
112 void ConferenceEditor::addClicked()
113 {
114     UrlInputDialog url_input(this);
115     switch (url_input.exec()) {
116     case UrlInputDialog::HaveUrl: emit haveConferenceUrl(url_input.url()); break;
117     case UrlInputDialog::HaveFile: emit haveConferenceFile(url_input.url()); break;
118     case UrlInputDialog::Cancel: return;
119     }
120 }
121
122 void ConferenceEditor::removeClicked()
123 {
124     if (selected_id < 0) {
125         // TODO: disable it when none is selected
126         return;
127     }
128
129     QMessageBox::StandardButton answer = 
130         QMessageBox::question(0
131             , "Deletion confirmation"
132             , QString("Really delete the %1 conference").arg(Conference::getById(selected_id).title())
133             , QMessageBox::Yes | QMessageBox::No
134             , QMessageBox::No);
135
136     if (answer == QMessageBox::Yes) {
137         emit removeConferenceRequested(selected_id);
138     }
139 }
140
141 void ConferenceEditor::changeUrlClicked()
142 {
143     if (selected_id < 0) {
144         return;
145     }
146     const Conference& selected = Conference::getById(selected_id);
147
148     bool ok;
149     QString url = QInputDialog::getText(this, "URL Input", "Enter schedule URL", QLineEdit::Normal, selected.url(), &ok);
150
151     if (ok) {
152         emit changeUrlRequested(selected_id, url);
153     }
154 }
155
156 void ConferenceEditor::refreshClicked()
157 {
158     if (selected_id < 0) {
159         return;
160     }
161     const Conference& selected = Conference::getById(selected_id);
162
163     QString url = selected.url();
164
165     if (!url.isEmpty()) {
166         emit haveConferenceUrl(url);
167     } else {
168         static const QString format("Schedule URL for %1 is not set. Enter the schedule URL:");
169         bool ok;
170         QString url = QInputDialog::getText(this, "URL Input", format.arg(selected.title()), QLineEdit::Normal, QString(), &ok);
171
172         if (ok) {
173             // first save it, to remain if fetch fails
174             emit changeUrlRequested(selected_id, url);
175             // then fetch
176             emit haveConferenceUrl(url);
177         }
178     }
179 }
180
181 void ConferenceEditor::importStarted()
182 {
183     addBtn->hide();
184     removeBtn->hide();
185     buttons->layout()->removeItem(buttonsSpacer);
186     progressBar->setValue(0);
187     progressBar->show();
188
189     QApplication::processEvents();
190 }
191
192 void ConferenceEditor::showParsingProgress(int progress)
193 {
194     progressBar->setValue(progress);
195
196     QApplication::processEvents();
197 }
198
199 void ConferenceEditor::importFinished(const QString& title)
200 {
201     qDebug() << __PRETTY_FUNCTION__ << title;
202     addBtn->show();
203     // removeItem should be shown later, but it takes some time,
204     // and not looks good
205     // anyway it will be shown a bit later
206     removeBtn->show();
207     buttons->layout()->addItem(buttonsSpacer);
208     progressBar->hide();
209
210     QApplication::processEvents();
211
212     int num = model->rowCount();
213     for (int i = 0; i < num; i++) {
214         QModelIndex item = model->index(i, 0);
215         if (model->data(item) == title) {
216             emit wantCurrent(item, QItemSelectionModel::SelectCurrent);
217             return;
218         }
219     }
220     itemSelected(QModelIndex(), QModelIndex());
221 }
222
223 void ConferenceEditor::conferenceMapClicked()
224 {
225     Conference conf = Conference::getById(selected_id);
226     QString mapPath = conf.map();
227     if(mapPath.isEmpty() or !QFile::exists(mapPath)) {
228         error_message("Map is not available");
229         return;
230     }
231
232     QString roomName;
233
234     QPixmap map(mapPath);
235     MapWindow window(map,roomName,this);
236     window.exec();
237 }