added the affiliation breaking functionality
[cewqo08.git] / mainwindow.cpp
blob33036478250cce07d05fba2e468917f5ff46c8da
1 /*
2 * Copyright (C) 2008 by Filip Brcic <brcha@gna.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "mainwindow.h"
20 #include <KApplication>
21 #include <KAction>
22 #include <KLocale>
23 #include <KActionCollection>
24 #include <KStandardAction>
25 #include <KFileDialog>
26 #include <KMessageBox>
27 #include <KIO/NetAccess>
28 #include <KSaveFile>
29 #include <QTextStream>
30 #include <KProgressDialog>
31 #include <QRegExp>
32 #include <KDebug>
34 MainWindow::MainWindow(QWidget *parent)
35 : KXmlGuiWindow(parent),
36 fileName(QString())
38 tableWidget = new QTableWidget();
39 tableWidget->setColumnCount(14);
41 QStringList header;
42 header << i18n("Title")
43 << i18n("Full name")
44 << i18n("State")
45 << i18n("Type of presentation")
46 << i18n("Topic")
47 << i18n("Accomodation")
48 << i18n("Fee")
49 << i18n("Staying")
50 << i18n("FEE")
51 << i18n("Abstract")
52 << i18n("Abstract Title")
53 << i18n("E-mail")
54 << i18n("Affiliation")
55 << i18n("Desired Presentation (with abstract)");
56 tableWidget->setHorizontalHeaderLabels(header);
58 setCentralWidget(tableWidget);
60 setupActions();
63 void MainWindow::setupActions()
65 KAction *clearAction = new KAction(this);
66 clearAction->setText(i18n("Clear"));
67 clearAction->setIcon(KIcon("edit-clear"));
68 clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
69 actionCollection()->addAction("clear", clearAction);
70 connect(clearAction, SIGNAL(triggered(bool)),
71 this, SLOT(clear()));
73 KAction *swapNamesAction = new KAction(this);
74 swapNamesAction->setText(i18n("Swap names and surnames"));
75 swapNamesAction->setIcon(KIcon("system-switch-user"));
76 swapNamesAction->setShortcut(Qt::CTRL + Qt::Key_N);
77 actionCollection()->addAction("swapNames", swapNamesAction);
78 connect(swapNamesAction, SIGNAL(triggered(bool)),
79 this, SLOT(swapNames()));
81 KAction *unpackAffiliationAction = new KAction(this);
82 unpackAffiliationAction->setText(i18n("Unpack the affiliation field"));
83 unpackAffiliationAction->setIcon(KIcon("application-x-compress"));
84 unpackAffiliationAction->setShortcut(Qt::CTRL + Qt::Key_U);
85 actionCollection()->addAction("unpackAffiliation", unpackAffiliationAction);
86 connect(unpackAffiliationAction, SIGNAL(triggered(bool)),
87 this, SLOT(unpackAffiliation()));
89 KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
90 KStandardAction::open(this, SLOT(openFile()), actionCollection());
91 KStandardAction::save(this, SLOT(saveFile()), actionCollection());
92 KStandardAction::saveAs(this, SLOT(saveFileAs()), actionCollection());
93 KStandardAction::openNew(this, SLOT(newFile()), actionCollection());
95 setupGUI();
98 void MainWindow::newFile()
100 fileName.clear();
101 clear();
104 void MainWindow::saveFileAs(const QString &outputFileName)
106 KSaveFile file(outputFileName);
107 file.open();
109 QByteArray outputByteArray;
110 QTextStream ts(&outputByteArray);
111 // outputByteArray.append(textArea->toPlainText().toUtf8());
112 for (int r=0; r < tableWidget->rowCount(); r++)
114 for (int c=0; c < tableWidget->columnCount(); c++)
116 if (c == 0)
117 ts << tableWidget->item(r,c)->text().toUtf8();
118 else
119 ts << "%" << tableWidget->item(r,c)->text().toUtf8();
121 ts << endl;
124 file.write(outputByteArray);
125 file.finalize();
126 file.close();
128 fileName = outputFileName;
131 void MainWindow::saveFileAs()
133 saveFileAs(KFileDialog::getSaveFileName());
136 void MainWindow::saveFile()
138 if (!fileName.isEmpty())
140 saveFileAs(fileName);
142 else
144 saveFileAs();
148 void MainWindow::openFile()
150 openFile(KFileDialog::getOpenFileName());
153 void MainWindow::openFile(const QString &inputFileName)
155 QString tmpFile;
156 QString currentLine;
157 if (KIO::NetAccess::download(inputFileName, tmpFile, this))
159 QFile file(tmpFile);
160 file.open(QIODevice::ReadOnly);
161 QTextStream ts(&file);
162 ts.setCodec("UTF-8");
163 ts.setAutoDetectUnicode(true);
164 ts.readLine(); ts.readLine(); // drop the first two lines as they are not data
165 while (!ts.atEnd())
167 currentLine = ts.readLine();
168 QStringList list = currentLine.split("%");
169 int currentRow = tableWidget->rowCount();
170 tableWidget->insertRow(currentRow);
171 for (int i=0; i < list.size(); i++)
172 tableWidget->setItem(currentRow, i, new QTableWidgetItem(list.at(i)));
174 // textArea->setPlainText(QTextStream(&file).readAll());
175 fileName = inputFileName;
177 KIO::NetAccess::removeTempFile(tmpFile);
179 else
181 KMessageBox::error(this, KIO::NetAccess::lastErrorString());
185 void MainWindow::swapNames()
187 KProgressDialog * pd = new KProgressDialog(this,
188 i18n("Swapping first and last names"),
189 i18n("Swapping first and last names"));
190 pd->setAllowCancel(false);
191 pd->progressBar()->setRange(0,tableWidget->rowCount()-1);
192 pd->show();
193 for (int r=0; r < tableWidget->rowCount(); r++)
195 pd->progressBar()->setValue(r);
196 QString currentName = tableWidget->item(r, 1)->text();
197 QStringList list = currentName.split(' '); // split the name in firstname and lastname
198 // for those who have more than 2 words in fullname, treat the last word as lastname
199 list.push_front(list.last());
200 list.pop_back();
201 tableWidget->item(r, 1)->setText(list.join(" "));
203 // resort the table by names
204 tableWidget->sortItems(1);
207 void MainWindow::clear()
209 tableWidget->clearContents();
210 tableWidget->setRowCount(0);
213 void MainWindow::unpackAffiliation()
215 tableWidget->insertColumn(12);
216 tableWidget->insertColumn(12);
217 tableWidget->insertColumn(12);
218 tableWidget->insertColumn(12);
219 tableWidget->insertColumn(12);
220 tableWidget->insertColumn(12);
222 QStringList header;
223 header << i18n("Title")
224 << i18n("Full name")
225 << i18n("State")
226 << i18n("Type of presentation")
227 << i18n("Topic")
228 << i18n("Accomodation")
229 << i18n("Fee")
230 << i18n("Staying")
231 << i18n("FEE")
232 << i18n("Abstract")
233 << i18n("Abstract Title")
234 << i18n("E-mail")
235 << i18n("Institution") //12
236 << i18n("Address") //13
237 << i18n("Zip code") //14
238 << i18n("City") //15
239 << i18n("Country") //16
240 << i18n("Phone") //17
241 << i18n("Fax") //18
242 << i18n("Desired Presentation (with abstract)");
243 tableWidget->setHorizontalHeaderLabels(header);
244 // Institution: Faculty of Physics, Adam Mickiewicz UniversityAddress: ul. Umultowska 85, City: Poznan, Zip code: 61-614, Country: Poland, Phone: +48 61 8295 274, Fax: +48 61 8257 018
245 // Institution: Friedrich-Schiller-Universitaet Jena Theoretisch-Physikalisches InstitutAddress: Max-Wien-Platz 1, City: Jena, Zip code: D-07743, Country: Germany, Phone: , Fax:
246 QRegExp rx("^Institution: (.*)Address: (.*), City: (.*), Zip code: (.*), Country: (.*), Phone: (.*), Fax:(.*)$");
247 KProgressDialog * pd = new KProgressDialog(this,
248 i18n("Unpacking affiliations"),
249 i18n("Unpacking affiliations"));
250 pd->setAllowCancel(false);
251 pd->progressBar()->setRange(0,tableWidget->rowCount()-1);
252 pd->show();
253 for (int r=0; r < tableWidget->rowCount(); r++)
255 pd->progressBar()->setValue(r);
256 QString currentAffiliation = tableWidget->item(r, 18)->text();
257 kDebug() << "Row: " << r << " affiliation: " << currentAffiliation << endl;
258 int pos = rx.indexIn(currentAffiliation);
259 if (pos > -1)
261 kDebug() << "Matched: " << rx.capturedTexts() << endl;
262 tableWidget->setItem(r,12,new QTableWidgetItem(rx.cap(1)));
263 tableWidget->setItem(r,13,new QTableWidgetItem(rx.cap(2)));
264 tableWidget->setItem(r,14,new QTableWidgetItem(rx.cap(3)));
265 tableWidget->setItem(r,15,new QTableWidgetItem(rx.cap(4)));
266 tableWidget->setItem(r,16,new QTableWidgetItem(rx.cap(5)));
267 tableWidget->setItem(r,17,new QTableWidgetItem(rx.cap(6)));
268 tableWidget->setItem(r,18,new QTableWidgetItem(rx.cap(7)));
270 else
271 kDebug() << "no match :(" << endl;