Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

readwriteformbase.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   readwriteformbase.cpp                                                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   Read the file COPYING for details.                                    *
00011  *                                                                         *
00012  *   copyright: (C) 2003 by Jan Mueller                                    *
00013  *   email: janmueller7@hotmail.com                                        *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 #include "readwriteformbase.h"
00018 #include <qwidget.h>
00019 #include <error.h>
00020 #include <qmessagebox.h>
00021 #include <qsqlcursor.h>
00022 #include <qaction.h>
00023 #include <qapplication.h>
00024 #include <qsettings.h>
00025 #include <qpixmap.h>
00026 #include <qfiledialog.h>
00027 #include <qlabel.h>
00028 #include <qintdict.h>
00029 #include "global.h"
00030 #include "mainwindowimpl.h"
00031 #include "linklistboxitem.h"
00032 #include "linklistbox.h"
00033 #include "filllinklistbox.h"
00034 #include "threadlinkevent.h"
00035 #include "urlevent.h"
00036 #include <qobjectlist.h>
00037 
00038 ReadWriteFormBase::ReadWriteFormBase(LitUrl u, QObject *receiver, QString slot, QWidget *parent, const char *name): ReadOnlyFormBase(u, receiver, slot, parent, name), rec_is_locked(false), valid(false) {
00039   initToolBar();
00040   connect(&threadtimer, SIGNAL(timeout()), SLOT(checkThreads()));
00041   threadtimer.start(200, false);
00042 }
00043 
00044 ReadWriteFormBase::~ReadWriteFormBase() {
00045   waitForThreads();
00046   unlockRec();
00047   delete rwtoolbar;
00048 }
00049 
00050 bool ReadWriteFormBase::threadsFinished() {
00051   FillLinkListBox *f;
00052   for(f=threads.first(); f; f=threads.next())
00053     if(!f->finished()) return false;
00054   return true;
00055 }
00056 
00057 void ReadWriteFormBase::waitForThreads() {
00058   FillLinkListBox *f;
00059   unsigned int i;
00060   do {
00061     i=0;
00062     for(f=threads.first(); f; f=threads.next())
00063       if(f->wait(100)) i++;
00064     qApp->processEvents();
00065     
00066     qDebug("ReadWriteFormBase::waitForThreads() - i="+QString().setNum(i));
00067     qDebug("threads.count()="+QString().setNum(threads.count()));
00068   } while(threads.count()>i);
00069 }
00070 
00071 void ReadWriteFormBase::checkThreads() {
00072   if(threadsFinished()) {
00073     setWorkingGui(false);
00074     //    threadtimer.stop();
00075   }
00076 }
00077 
00078 /*
00079   Locking:
00080   Locking has two levels:
00081   external locking: Can this process lock the tuple or is it already locked 
00082   by another process?
00083   internal locking: Is the tuple already locked by this process and 
00084   displayed in another window?
00085   */
00086 bool ReadWriteFormBase::lockRec() {
00087   qDebug("lockRec()");
00088   if(rec_is_locked) {
00089     setReadonlyGui(false);
00090     return true;
00091   }
00092     
00093   // Check whether the tuple is internaly locked by another window...
00094   bool soft_locked=locked_recs_dict.find(no(url()).toLong());
00095   if(soft_locked) {
00096     qDebug("ReadWriteFormBase::lockRec(): tuple locked by other window.");         
00097     setReadonlyGui(true);
00098     return false;       
00099   }
00100     
00101   // Check whether the tuple is locked by another process...
00102   QSqlQuery query( "select lock_tuple(oid) from "+url().table()+" where "+url().query());
00103   query.first();
00104   bool db_locked=(query.value(0).toString()=="false");
00105   if(db_locked) {
00106     qDebug("ReadWriteFormBase::lockRec(): tuple locked by another process.");
00107     setReadonlyGui(true);
00108     return false;
00109   }
00110     
00111   qDebug("ReadWriteFormBase::lockRec(): tuple not locked by another process or another window.");
00112     
00113   locked_recs_dict.insert(no(url()).toLong(), new bool(true));
00114   setReadonlyGui(false);
00115   return true;
00116 }
00117  
00118 void ReadWriteFormBase::unlockRec() {
00119   if(!rec_is_locked) return;
00120   locked_recs_dict.remove(no(url()).toLong());
00121   QSqlQuery query( "select unlock_tuple(oid) from "+url().table()+" where "+url().query());
00122   rec_is_locked=false;
00123 }
00124 
00125 void ReadWriteFormBase::lockGui() {
00126   const QObjectList *list=children();
00127         
00128   QObjectListIt it(*list);
00129   for ( ; it.current(); ++it ) {
00130     if(it.current()->isWidgetType()) {
00131       QWidget *w=dynamic_cast<QWidget*>(it.current());
00132       w->setEnabled(false);
00133     }
00134   }    
00135 }
00136 
00137 void ReadWriteFormBase::unlockGui() {
00138   const QObjectList *list=children();
00139         
00140   QObjectListIt it(*list);
00141   for ( ; it.current(); ++it ) {
00142     if(it.current()->isWidgetType()) {
00143       QWidget *w=dynamic_cast<QWidget*>(it.current());
00144       w->setEnabled(true);
00145     }
00146   }    
00147 }
00148 
00149 void ReadWriteFormBase::copy() {
00150 }
00151 
00152 void ReadWriteFormBase::cut() {
00153 }
00154 
00155 void ReadWriteFormBase::paste() {
00156 }
00157 
00158 
00159 void ReadWriteFormBase::write()
00160 { 
00161   if(!isValid()) return;
00162   if(formcursor.position("created")>-1) formcursor.remove(formcursor.position("created"));
00163   if(formcursor.position("modified")>-1) formcursor.remove(formcursor.position("modified"));
00164 
00165   form.writeFields();
00166   if(formcursor.primaryIndex().isEmpty()) throw Error("No primary Index!");
00167   if(!formcursor.update())
00168     QMessageBox::warning(this, "Error", "Can't update database!");
00169 }
00170 
00171 void ReadWriteFormBase::deleteRec()
00172 {
00173   if(!isValid()) return;
00174   if(QMessageBox::information( this, "Literature",
00175                                "Do you want to delete the record?",
00176                                "&Yes", "&No")!=0) return;
00177                 
00178   if(!rec_is_locked) return;
00179   QSqlQuery query( "select del_obj(no) from "+url().table()+" where "+url().query());
00180   query.first();
00181   if(query.value(0).toString()!="true")
00182     QMessageBox::warning(this, "Error", "Can't delete record since one or more depending records are locked!"+query.value(0).toString());
00183   else {
00184     setInvalidGui(true);
00185     setValid(false);
00186   }
00187 }
00188 
00189 void ReadWriteFormBase::setWorkingGui(bool work) 
00190 {
00191   rwtoolbar->setEnabled(!work);
00192   emit workingGui(work);
00193 }
00194 
00195 void ReadWriteFormBase::setReadonlyGui(bool ro)
00196 {
00197   debug("ReadWriteFormBase::setReadonlyGui(bool ro)");
00198   emit readonlyGui(ro);
00199 }
00200 
00201 void ReadWriteFormBase::setInvalidGui(bool inv) 
00202 {
00203   writeAction->setEnabled(!inv);
00204   deleteAction->setEnabled(!inv);
00205   selBackgrAction->setEnabled(!inv);
00206   emit invalidGui(inv);
00207 }
00208 
00209 void ReadWriteFormBase::customEvent(QCustomEvent *e) {
00210   ThreadLinkEvent *l=dynamic_cast<ThreadLinkEvent*>(e);
00211   if(l && l->parentItem())
00212     new LinkListBoxItem(l->parentItem(), l->text(), l->url(), l->pixmap());
00213   else if(l) 
00214     new LinkListBoxItem(l->box(), l->text(), l->url(), l->pixmap());
00215 }
00216 
00217 bool ReadWriteFormBase::setData(void) {
00218   // we can't call lockRec() in the constructor since it calls setxxxxGui(), which is virtual und reimplemented by derived classes and not vailable in the constructor.
00219   rec_is_locked=lockRec();
00220   return true;
00221 }
00222 
00223 void ReadWriteFormBase::initToolBar() {
00224   rwtoolbar=new QToolBar(mainWindow());
00225     
00226   writeAction=new QAction("Write", QIconSet(loadPixmap("floppy.png")), "&Write", Qt::CTRL+Key_W, this, "write");
00227   connect(writeAction, SIGNAL(activated()), this, SLOT(write()));
00228   deleteAction=new QAction("Delete", QIconSet(loadPixmap("delete.png")), "&Delete", Qt::CTRL+Key_D, this, "delete");
00229   connect(deleteAction, SIGNAL(activated()), this, SLOT(deleteRec()));
00230   writeAction->addTo(rwtoolbar);
00231   deleteAction->addTo(rwtoolbar);
00232   rwtoolbar->show();
00233 
00234   writeAction->addTo(roMenu());
00235   deleteAction->addTo(roMenu());
00236 }
00237 
00238 void ReadWriteFormBase::createLink(LitUrl obj1, LitUrl obj2) {
00239   write();
00240   QSqlQuery q("insert into link (obj_1, tab_1, obj_2, tab_2) values ("+no(obj1)+",tabid('"+obj1.table()+"'),"+no(obj2)+",tabid('"+obj2.table()+"'))");
00241   ASSERT(url().isValid());
00242   if(!setData())
00243     setInvalidGui(true);
00244 }
00245 
00246 
00247 void ReadWriteFormBase::createPublicationAuthor(LitUrl publ, LitUrl author) {
00248   write();
00249   QSqlQuery q("select max(author_order) from publication_author where publication_no="+no(publ));
00250   q.first();
00251   int o=q.value(0).toInt();
00252   q.exec("insert into publication_author (publication_no, author_no, author_order) values ("+no(publ)+","+no(author)+","+QString().setNum(o+1)+")");
00253   ASSERT(url().isValid());
00254   if(!setData())
00255     setInvalidGui(true);
00256 }
00257 
00258 void ReadWriteFormBase::createProjectBibliography(LitUrl pr, LitUrl pub) {
00259   write();
00260   QSqlQuery q("insert into project_bibliography (project_no, id_no, tab_no) values ("+no(pr)+","+no(pub)+","+tab(pub)+")");
00261   ASSERT(url().isValid());
00262   if(!setData())
00263     setInvalidGui(true);
00264 }
00265 
00266 void ReadWriteFormBase::deleteProjectBibliography(LitUrl pr, LitUrl publ) {
00267   write();
00268   QSqlQuery q("delete from project_bibliography where project_no="+no(publ)+" and id_no="+no(publ)+" and tab_no="+tab(publ)+")");
00269   ASSERT(url().isValid());
00270   if(!setData())
00271     setInvalidGui(true);
00272 }
00273 
00274 
00275 void ReadWriteFormBase::createPartAuthor(LitUrl part, LitUrl author) {
00276   write();
00277   QSqlQuery q("select max(author_order) from part_author where part_no="+no(part));
00278   q.first();
00279   int o=q.value(0).toInt();
00280   
00281   q.exec("insert into part_author (part_no, author_no, author_order) values ("+no(part)+","+no(author)+","+QString().setNum(o+1)+")");
00282   ASSERT(url().isValid());
00283   if(!setData())
00284     setInvalidGui(true);
00285 }
00286 
00287 void ReadWriteFormBase::deletePublicationAuthor(LitUrl publ, LitUrl author) {
00288   write();
00289   QSqlQuery q("delete from publication_author where publication_no="+no(publ)+" and author_no="+no(author));
00290   ASSERT(url().isValid());
00291   if(!setData())
00292     setInvalidGui(true);
00293 }
00294 
00295 void ReadWriteFormBase::deletePartAuthor(LitUrl part, LitUrl author) {
00296   write();
00297   QSqlQuery q("delete from part_author where part_no="+no(part)+" and author_no="+no(author));
00298   ASSERT(url().isValid());
00299   if(!setData())
00300     setInvalidGui(true);
00301 }
00302 
00303 void ReadWriteFormBase::deleteLink(LitUrl obj1, LitUrl obj2) {
00304   write();
00305   QSqlQuery q("delete from link where (obj_1="+no(obj1)+" and obj_2="+no(obj2)+" and tab_1=tabid('"+obj1.table()+"') and tab_2=tabid('"+obj2.table()+"')) or (obj_2="+no(obj1)+" and obj_1="+no(obj2)+" and tab_2=tabid('"+obj1.table()+"') and tab_1=tabid('"+obj2.table()+"'))");
00306   ASSERT(url().isValid());
00307   if(!setData())
00308     setInvalidGui(true);
00309 }
00310 
00311 void ReadWriteFormBase::newNote(LitUrl obj) {
00312   QSqlQuery q("select create_publ_note("+no(obj)+") as no");
00313   q.first();
00314   QString n=q.value(0).toString();
00315   q.exec("insert into link (obj_1, tab_1, obj_2, tab_2) values ("+n+",tabid('note'),"+no(url())+",tabid('"+url().table()+"'))");
00316 
00317   qApp->postEvent(topLevelWidget(), new UrlEvent(LitUrl("lit://note/note?no="+n), false));
00318 }
00319  

Generated on Sat Mar 27 19:20:43 2004 for Literature by doxygen 1.3.6-20040222