00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "part.h"
00018 #include "liturl.h"
00019 #include <qlayout.h>
00020 #include <qtoolbar.h>
00021 #include <qaction.h>
00022 #include <qpixmap.h>
00023 #include <qmessagebox.h>
00024 #include <qlineedit.h>
00025 #include <qlabel.h>
00026 #include <qapplication.h>
00027 #include <qsqldatabase.h>
00028 #include <qsqlcursor.h>
00029 #include <qevent.h>
00030 #include <qcombobox.h>
00031 #include <qsettings.h>
00032 #include "error.h"
00033 #include "editorhelper.h"
00034 #include "global.h"
00035 #include "linklistbox.h"
00036 #include "authorlinklistbox.h"
00037 #include "filllinklistbox.h"
00038 #include "mainwindowimpl.h"
00039 #include <qlayout.h>
00040 #include <qpushbutton.h>
00041 #include <qfiledialog.h>
00042 #include <qprogressdialog.h>
00043 #include "urlevent.h"
00044 #include "bibtexfactory.h"
00045 #include "selectnote.h"
00046 #include "selectitem.h"
00047 #include "convert2text.h"
00048 #include <importprogress.h>
00049
00050
00051
00052 #ifdef HAS_KDE
00053 #include <kmimemagic.h>
00054 #include <qcstring.h>
00055 #include <kmdcodec.h>
00056 #endif
00057
00058 Part::Part(LitUrl url, QObject *receiver, QString slot, MainWindow *parent, const char *name): ReadWriteFormBase(url, receiver, slot, parent, name), toolbar(0L) {
00059 partform=new PartForm(this, "partform");
00060 QHBoxLayout *l=new QHBoxLayout(this);
00061 l->addWidget(partform);
00062
00063 QSettings settings;
00064 settings.setPath("www.test.de", "Literature");
00065 QString backgr = settings.readEntry(QString("/literature/backgr_")+name, "");
00066 if(!backgr.isEmpty()) partform->setErasePixmap(QPixmap(backgr));
00067 connect(partform->genbibtex, SIGNAL(clicked()), this, SLOT(generateBibTex()));
00068
00069 initToolBar();
00070 setData();
00071 }
00072
00073 void Part::initToolBar() {
00074 toolbar=new QToolBar(mainWindow());
00075
00076 showPartMemoAction=new QAction("Show Memo", QIconSet(loadPixmap("showmemo.png")), "Show Memo", 0, this, "showmemo");
00077 connect(showPartMemoAction, SIGNAL(activated()), this, SLOT(showPartMemo()));
00078
00079 showPartPublAction=new QAction("Show Publication", QIconSet(loadPixmap("showpubl.png")), "Show Publication", 0, this, "showpartpubl");
00080 connect(showPartPublAction, SIGNAL(activated()), this, SLOT(showPartPubl()));
00081
00082
00083 insertNoteAction=new QAction("Insert Note", QIconSet(loadPixmap("insnote.png")), "Insert Note", 0, this, "insertnote");
00084 connect(insertNoteAction, SIGNAL(activated()), this, SLOT(insertNote()));
00085
00086 createNoteAction=new QAction("Create Note", QIconSet(loadPixmap("newnote.png")), "Create Note", 0, this, "createnote");
00087 connect(createNoteAction, SIGNAL(activated()), this, SLOT(createNote()));
00088
00089 deleteNoteAction=new QAction("Delete Note", QIconSet(loadPixmap("delnote.png")), "Delete Note", 0, this, "deletenote");
00090 connect(deleteNoteAction, SIGNAL(activated()), this, SLOT(deleteNote()));
00091
00092
00093 insertAuthorAction=new QAction("Insert Author", QIconSet(loadPixmap("insauthor.png")), "Insert Author", 0, this, "insertauthor");
00094 connect(insertAuthorAction, SIGNAL(activated()), this, SLOT(insertAuthor()));
00095 createAuthorAction=new QAction("Create Author", QIconSet(loadPixmap("newauthor.png")), "Create Author", 0, this, "createauthor");
00096 connect(createAuthorAction, SIGNAL(activated()), this, SLOT(createAuthor()));
00097 deleteAuthorAction=new QAction("Delete Author", QIconSet(loadPixmap("delauthor.png")), "Delete Author", 0, this, "deleteauthor");
00098 connect(deleteAuthorAction, SIGNAL(activated()), this, SLOT(deleteAuthor()));
00099
00100 importFilesAction=new QAction("Import Files", QIconSet(loadPixmap("gnome-folder.png")), "Import Files", 0, this, "importfiles");
00101 connect(importFilesAction, SIGNAL(activated()), this, SLOT(importFiles()));
00102
00103 addToProjectAction=new QAction("Add to Project", QIconSet(loadPixmap("gnome-folder.png")), "Add to Project", 0, this, "addtoproject");
00104 connect(addToProjectAction, SIGNAL(activated()), this, SLOT(addToProject()));
00105
00106
00107 showPartPublAction->addTo(toolbar);
00108 showPartMemoAction->addTo(toolbar);
00109
00110 insertNoteAction->addTo(toolbar);
00111 createNoteAction->addTo(toolbar);
00112 deleteNoteAction->addTo(toolbar);
00113
00114 insertAuthorAction->addTo(toolbar);
00115 createAuthorAction->addTo(toolbar);
00116 deleteAuthorAction->addTo(toolbar);
00117
00118 importFilesAction->addTo(toolbar);
00119 addToProjectAction->addTo(toolbar);
00120
00121 toolbar->show();
00122
00123 showPartPublAction->addTo(roMenu());
00124 showPartMemoAction->addTo(roMenu());
00125
00126 insertNoteAction->addTo(roMenu());
00127 createNoteAction->addTo(roMenu());
00128 deleteNoteAction->addTo(roMenu());
00129
00130 insertAuthorAction->addTo(roMenu());
00131 createAuthorAction->addTo(roMenu());
00132 deleteAuthorAction->addTo(roMenu());
00133
00134 importFilesAction->addTo(roMenu());
00135 addToProjectAction->addTo(roMenu());
00136
00137 }
00138
00139 Part::~Part() {
00140 write();
00141 delete toolbar;
00142 }
00143
00144 bool Part::setData()
00145 {
00146 try {
00147 ReadWriteFormBase::setData();
00148 LitUrl u=url();
00149 setWorkingGui(true);
00150 form.clear();
00151 formcursor.clear();
00152 formcursor.setMode(QSqlCursor::Writable);
00153 formcursor.setName("part");
00154 if(!formcursor.select(u.query()))
00155 throw(Error(formcursor.lastError()));
00156 if(formcursor.size()!=1) throw Error("Invalid Record!");
00157 formcursor.first();
00158 QString no=formcursor.value("no").toString();
00159 form.setRecord(formcursor.primeUpdate());
00160 form.insert(partform->number, "partno");
00161 form.insert(partform->title, "title");
00162 form.insert(partform->key, "key");
00163 form.insert(partform->pages, "pages");
00164 form.insert(partform->bibtex, "bibtex");
00165 form.insert(partform->modified, "modified");
00166 form.insert(partform->created, "created");
00167 form.readFields();
00168
00169 partform->links->setEnabled(false);
00170 partform->authors->setEnabled(false);
00171 partform->files->setEnabled(false);
00172
00173 partform->authors->clear();
00174 fillauthor=new FillLinkListBox(this, "partauthorfllb", partform->authors, "select author.no, author.firstname, author.lastname from author, part_author where author.no=part_author.author_no and part_author.part_no="+no+" order by part_author.author_order desc", "author/author", loadPixmap("showauthor.png"));
00175 fillauthor->start();
00176 threads.append(fillauthor);
00177
00178 partform->files->clear();
00179 fillfiles=new FillLinkListBox(this, "filesfllb", partform->files, "select partdata.no, partdata.filename from partdata where partdata.part_no="+no+" order by partdata.filename","file/partdata", loadPixmap("showfile.png"));
00180 fillfiles->start();
00181 threads.append(fillfiles);
00182
00183 partform->links->clear();
00184 filllink=new FillLinkListBox(this, "linkpartfllb", partform->links, "select note.no, note.title from note, link where"+buildLinkWhereClause("note", "part", no), "note/note", loadPixmap("shownote.png"));
00185 filllink->start();
00186 threads.append(filllink);
00187
00188 setValid(true);
00189 } catch(Error &e) {
00190 setValid(false);
00191 setReadonlyGui(true);
00192 QMessageBox::warning(this, "Error", e.message());
00193 return false;
00194 }
00195 setValid(true);
00196 return true;
00197 }
00198
00199 void Part::write() {
00200 ReadWriteFormBase::write();
00201 if(!partform->authors->modified()) return;
00202 typedef QValueList<LitUrl> List;
00203 List l=partform->authors->authorPos();
00204
00205 List::iterator it;
00206 int i=0;
00207 for ( it = l.begin(); it != l.end(); ++it ) {
00208 QString n=no(*it);
00209 QSqlQuery("update part_author set author_order="+QString().setNum(i)+" where part_no="+no(url())+" and author_no="+n);
00210 i++;
00211 }
00212 }
00213
00214
00215 void Part::setWorkingGui(bool work) {
00216 ReadWriteFormBase::setInvalidGui(work);
00217 partform->links->setEnabled(!work);
00218 partform->authors->setEnabled(!work);
00219 partform->files->setEnabled(!work);
00220 }
00221
00222 void Part::setReadonlyGui(bool ro) {
00223 ReadWriteFormBase::setReadonlyGui(ro);
00224 partform->title->setEnabled(!ro);
00225 partform->bibtex->setEnabled(!ro);
00226 partform->key->setEnabled(!ro);
00227 partform->number->setEnabled(!ro);
00228 partform->pages->setEnabled(!ro);
00229 }
00230
00231 void Part::setInvalidGui(bool inv) {
00232 ReadWriteFormBase::setInvalidGui(inv);
00233 setWorkingGui(false);
00234 setReadonlyGui(inv);
00235 }
00236
00237 void Part::showPartMemo() {
00238 qApp->postEvent(topLevelWidget(), new UrlEvent(LitUrl("lit://partmemo/part?no="+no(url())), false));
00239 }
00240
00241 void Part::generateBibTex() {
00242
00243
00244
00245 debug("Part::generateBibTex()");
00246 PartBibTexFactory f(formcursor);
00247 debug(f.getEntry());
00248 partform->bibtex->setText(f.getEntry());
00249 }
00250
00251
00252 void Part::insertNote() {
00253 SelectItem *s=new SelectItem(this, "selnote");
00254 Q_ASSERT(s);
00255 s->setData("note/note", "select note.no, note.title from note, part where note.publication_no=part.publication_no and part.no="+no(url())+" order by note.title");
00256 if(s->exec()==QDialog::Accepted)
00257 createLink(s->url(), url());
00258 }
00259
00260 void Part::deleteNote() {
00261 deleteLink(partform->links->url(), url());
00262 }
00263
00264 void Part::createNote() {
00265 QSqlQuery q("select publication_no from part where no="+no(url()));
00266 q.first();
00267 newNote(LitUrl("lit://publication/publication?no="+q.value(0).toString()));
00268 }
00269
00270 void Part::insertAuthor() {
00271 SelectItem *s=new SelectItem(this, "selauthor");
00272 Q_ASSERT(s);
00273 s->setData("author/author", "select no, lastname, firstname from author order by lastname");
00274 if(s->exec()==QDialog::Accepted)
00275 createPartAuthor(url(), s->url());
00276 }
00277
00278 void Part::addToProject() {
00279 SelectItem *s=new SelectItem(this, "selproject");
00280 Q_ASSERT(s);
00281 s->setData("project/project", "select no, title from project order by title");
00282 if(s->exec()==QDialog::Accepted) {
00283 QSqlQuery q("insert into project_bibliography (project_no, tab_no, id_no) values ("+
00284 no(s->url())+",tabid('part'), "+no(url())+")");
00285 }
00286 }
00287
00288 void Part::deleteAuthor() {
00289 deletePartAuthor(url(), partform->authors->url());
00290 }
00291
00292 void Part::createAuthor() {
00293 QSqlQuery q("select create_author() as no");
00294 q.first();
00295 LitUrl u("lit://author/author?no="+q.value(0).toString());
00296 createPartAuthor(url(), u);
00297 qApp->postEvent(topLevelWidget(), new UrlEvent(u, false));
00298 }
00299
00300 void Part::showPartPubl() {
00301 LitUrl u("lit://publication/publication?no="+formcursor.value("publication_no").toString());
00302 qApp->postEvent(topLevelWidget(), new UrlEvent(u, false));
00303 }
00304
00305
00306 void Part::writebuf(QByteArray buf, int chunkno, QString fileno) {
00307
00308 QFile f("/tmp/writebuf"+QString().setNum(chunkno));
00309 f.open(IO_WriteOnly);
00310 f.writeBlock(buf);
00311 f.close();
00312
00313 QSqlQuery q;
00314 debug("importing chunk"+QString().setNum(chunkno)+" with partdata_no="+fileno);
00315
00316 QCString b64=KCodecs::base64Encode(qCompress(buf));
00317
00318 q.prepare("insert into file (chunk, partdata_no, chunk_no) values ('"+b64+"', "+QCString(fileno)+", "+QCString().setNum(chunkno)+")");
00319
00320 q.exec();
00321 }
00322
00323
00324 #define bufsize 1000000
00325
00326 void Part::importFiles() {
00327 QStringList files = QFileDialog::getOpenFileNames("*",
00328 "/home/",
00329 this,
00330 "Open files dialog"
00331 "Select one or more files" );
00332 QStringList list = files;
00333 QStringList::Iterator it = list.begin();
00334 QString mimetype;
00335 QString pno=no(url());
00336
00337 ImportProgress progress(this, "importprogress");
00338 progress.show();
00339 progress.all->setTotalSteps(list.count());
00340 progress.all->setProgress(0);
00341
00342 int i=0;
00343 while(it!=list.end()) {
00344 try {
00345 progress.filename->setText(*it);
00346 QFile f(*it);
00347 progress.file->setTotalSteps(f.size()/bufsize+1);
00348 progress.file->setProgress(0);
00349 f.open(IO_ReadOnly);
00350 QByteArray buf=f.readAll();
00351
00352 Convert2Text conv;
00353 QString text;
00354
00355 try {
00356 conv.mime2text(*it, text);
00357 } catch(Error &e) {
00358 QMessageBox::warning(this, "Error", e.message());
00359 }
00360
00361 QSqlQuery q("select create_partdata("+pno+")");
00362 q.first();
00363 QString partdata_no=q.value(0).toString();
00364 debug("partdata_no="+partdata_no);
00365 q.prepare("update partdata set filename=?, type='"+mimetype+"', astext=? where no="+partdata_no);
00366 q.bindValue(0, QUrl(*it).fileName());
00367 q.bindValue(1, text.simplifyWhiteSpace());
00368 q.exec();
00369
00370 QByteArray chunk(bufsize);
00371
00372 unsigned int j=0;
00373 while((j+1)*bufsize<buf.size()) {
00374 memcpy(chunk.data(), buf.data()+j*bufsize, bufsize);
00375 j++;
00376 writebuf(chunk, j, partdata_no);
00377 progress.file->setProgress(j);
00378 qApp->processEvents();
00379 }
00380 chunk.resize(buf.size()-j*bufsize);
00381 memcpy(chunk.data(), buf.data()+j*bufsize, buf.size()-j*bufsize);
00382 writebuf(chunk, j+1, partdata_no);
00383
00384 f.close();
00385
00386 progress.all->setProgress(i);
00387
00388
00389 qApp->processEvents();
00390 i++;
00391 ++it;
00392 } catch(Error &e) {
00393 QMessageBox::warning(this, "Error", e.message());
00394 }
00395 }
00396 progress.all->setProgress(list.count());
00397 }