00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "file.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 <qlayout.h>
00031 #include <qsettings.h>
00032 #include <qprogressdialog.h>
00033 #include "error.h"
00034 #include "editorhelper.h"
00035 #include "global.h"
00036 #include "linklistbox.h"
00037 #include "filllinklistbox.h"
00038 #include "mainwindowimpl.h"
00039 #include "selectitem.h"
00040 #include "selectnote.h"
00041 #include "urlevent.h"
00042 #include <qfile.h>
00043 #include <qsplitter.h>
00044
00045 #ifdef HAS_KDE
00046 #include <part.h>
00047 #include <ktrader.h>
00048 #include <kmimemagic.h>
00049 #include <kservice.h>
00050 #include <kmdcodec.h>
00051 #include <kio/netaccess.h>
00052 #include <klibloader.h>
00053 #include <rundialog.h>
00054 #include <kmdcodec.h>
00055 #endif
00056
00057 File::File(LitUrl url, QObject *receiver, QString slot, MainWindow *parent, const char *name): ReadWriteFormBase(url, receiver, slot, parent, name), toolbar(0L), part(0L) {
00058 fileform=new FileForm(this, "fileform");
00059 QHBoxLayout *l=new QHBoxLayout(this);
00060 l->addWidget(fileform);
00061 initToolBar();
00062 new EditorHelper(this, fileform->memo);
00063 QSettings settings;
00064 QString backgr = settings.readEntry(QString("/literature/backgr_")+name, "");
00065 if(!backgr.isEmpty()) fileform->setErasePixmap(QPixmap(backgr));
00066 setData();
00067 }
00068
00069 void File::initToolBar() {
00070 toolbar=new QToolBar(mainWindow());
00071
00072 showPartAction=new QAction("Show Part", QIconSet(loadPixmap("showpart")), "Show Part", 0, this, "showpart");
00073 connect(showPartAction, SIGNAL(activated()), this, SLOT(showPart()));
00074
00075 insertNoteAction=new QAction("Insert Note", QIconSet(loadPixmap("insnote.png")), "Insert Note", 0, this, "insertnote");
00076 connect(insertNoteAction, SIGNAL(activated()), this, SLOT(insertNote()));
00077
00078 createNoteAction=new QAction("Create Note", QIconSet(loadPixmap("newnote.png")), "Create Note", 0, this, "createnote");
00079 connect(createNoteAction, SIGNAL(activated()), this, SLOT(createNote()));
00080
00081 deleteNoteAction=new QAction("Delete Note", QIconSet(loadPixmap("delnote.png")), "Delete Note", 0, this, "deletenote");
00082 connect(deleteNoteAction, SIGNAL(activated()), this, SLOT(deleteNote()));
00083
00084 runApplicationAction=new QAction("Run Application", QIconSet(loadPixmap("run.png")), "Run Application", 0, this, "runapplication");
00085 connect(runApplicationAction, SIGNAL(activated()), this, SLOT(runApp()));
00086
00087 addToProjectAction=new QAction("Add to Project", QIconSet(loadPixmap("gnome-folder.png")), "Add to Project", 0, this, "addtoproject");
00088 connect(addToProjectAction, SIGNAL(activated()), this, SLOT(addToProject()));
00089
00090 nextFileAction=new QAction("Next File", QIconSet(loadPixmap("next")), "Next File", 0, this, "nextfile");
00091 connect(nextFileAction, SIGNAL(activated()), this, SLOT(nextFile()));
00092
00093 prevFileAction=new QAction("Previous File", QIconSet(loadPixmap("previous")), "Previous File", 0, this, "prevfile");
00094 connect(prevFileAction, SIGNAL(activated()), this, SLOT(prevFile()));
00095
00096 insertNoteAction->addTo(toolbar);
00097 createNoteAction->addTo(toolbar);
00098 deleteNoteAction->addTo(toolbar);
00099 runApplicationAction->addTo(toolbar);
00100 addToProjectAction->addTo(toolbar);
00101 prevFileAction->addTo(toolbar);
00102 nextFileAction->addTo(toolbar);
00103 showPartAction->addTo(toolbar);
00104 toolbar->show();
00105
00106 insertNoteAction->addTo(roMenu());
00107 createNoteAction->addTo(roMenu());
00108 deleteNoteAction->addTo(roMenu());
00109 runApplicationAction->addTo(roMenu());
00110 addToProjectAction->addTo(roMenu());
00111 prevFileAction->addTo(roMenu());
00112 nextFileAction->addTo(roMenu());
00113 showPartAction->addTo(roMenu());
00114
00115 }
00116
00117 File::~File() {
00118 write();
00119 QSqlQuery query("select part_no from partdata where no="+no(url()));
00120 query.first();
00121 QString partno=query.value(0).toString();
00122 query.prepare("update part set memo=? where no="+partno);
00123 query.bindValue(0, fileform->memo->text());
00124 query.exec();
00125 delete toolbar;
00126 }
00127
00128 bool File::setData()
00129 {
00130 try {
00131 ReadWriteFormBase::setData();
00132 LitUrl u=url();
00133 setWorkingGui(true);
00134
00135 QSqlQuery query("select part.memo from part, partdata where part.no=partdata.part_no and partdata.no="+no(url()));
00136 Q_ASSERT(query.first());
00137 fileform->memo->setText(query.value(0).toString());
00138
00139 form.clear();
00140 formcursor.clear();
00141 formcursor.setMode(QSqlCursor::Writable);
00142 formcursor.setName("partdata");
00143
00144 if(!formcursor.select(u.query()))
00145 throw(Error(formcursor.lastError()));
00146
00147 if(formcursor.size()!=1) throw Error("Invalid Record!");
00148 formcursor.first();
00149
00150 QString no=formcursor.value("no").toString();
00151 form.setRecord(formcursor.primeUpdate());
00152 form.insert(fileform->filename, "filename");
00153
00154
00155 form.insert(fileform->modified, "modified");
00156 form.insert(fileform->created, "created");
00157 form.readFields();
00158
00159 #ifdef HAS_KDE
00160 file="/tmp/test";
00161 QFile f(file);
00162 f.open(IO_WriteOnly | IO_Truncate);
00163
00164 QSqlQuery q("select no from file where partdata_no="+no+" order by chunk_no");
00165 QProgressDialog progress( "Reading file...", "Abort reading", q.size(),
00166 this, "progress", TRUE );
00167
00168 int i=0;
00169 while(q.next()) {
00170 qApp->processEvents();
00171 QSqlQuery p("select chunk from file where no="+q.value(0).toString());
00172 p.first();
00173 QByteArray buf;
00174
00175
00176 KCodecs::base64Decode(p.value(0).asCString(), buf);
00177
00178 debug("Buf Size="+QString().setNum(buf.count()));
00179 QByteArray uncompr=qUncompress(buf);
00180
00181
00182 f.writeBlock(uncompr);
00183 progress.setProgress(i);
00184 i++;
00185 QFile d("/tmp/block"+QString().setNum(i));
00186 d.open(IO_WriteOnly);
00187 d.writeBlock(uncompr);
00188 d.close();
00189
00190 }
00191 f.close();
00192
00193 KMimeMagicResult *mime;
00194
00195 if(url().protocol()=="lit") {
00196 try {
00197 mime=KMimeMagic::self()->findFileType(file);
00198 } catch(...){
00199 QMessageBox::critical(this, "Error", "File does not exist!");
00200 return false;
00201 }
00202 }
00203 else {
00204 if (KIO::NetAccess::download(url(), kio_target) == false) {
00205 QMessageBox::critical(this, "Error", "File does not exist!");
00206 return false;
00207 }
00208 mime=KMimeMagic::self()->findFileType(kio_target);
00209 file=kio_target;
00210 }
00211
00212 KTrader::OfferList offers = KTrader::self()->query(mime->mimeType(), "'KParts/ReadOnlyPart' in ServiceTypes");
00213 if(offers.count()>0) {
00214 KService::Ptr ptr = offers.first();
00215 if(ptr) {
00216 KLibFactory *factory = KLibLoader::self()->factory(ptr->library());
00217
00218 if (factory) {
00219 QValueList<int> list = fileform->splitter->sizes();
00220 delete part; part=0L;
00221 part = (KParts::ReadOnlyPart *)factory->create(fileform->splitter, ptr->name(), "KParts::ReadOnlyPart");
00222 fileform->splitter->setSizes(list);
00223 part->openURL(file);
00224 part->widget()->show();
00225
00226 }
00227 }
00228 }
00229 #endif
00230
00231 formcursor.remove(formcursor.position("astext"));
00232
00233
00234 fileform->links->clear();
00235 filllink=new FillLinkListBox(this, "linkfilefllb", fileform->links, "select note.no, note.title from note, link where"+buildLinkWhereClause("note", "partdata", no)+" order by note.title","note/note", loadPixmap("shownote.png"));
00236 filllink->start();
00237 threads.append(filllink);
00238
00239 setValid(true);
00240 } catch(Error &e) {
00241 setValid(false);
00242 setReadonlyGui(true);
00243 QMessageBox::warning(this, "Error", e.message());
00244 return false;
00245 }
00246 setValid(true);
00247 return true;
00248 }
00249
00250 void File::setWorkingGui(bool work) {
00251 ReadWriteFormBase::setWorkingGui(work);
00252 fileform->links->setEnabled(!work);
00253 }
00254
00255 void File::setReadonlyGui(bool ro) {
00256 debug("File::setReadonlyGui");
00257 ReadWriteFormBase::setReadonlyGui(ro);
00258 fileform->filename->setEnabled(!ro);
00259 }
00260
00261 void File::setInvalidGui(bool inv) {
00262 ReadWriteFormBase::setInvalidGui(inv);
00263 setWorkingGui(false);
00264 setReadonlyGui(inv);
00265 }
00266
00267 void File::insertNote() {
00268 SelectNote *s=new SelectNote(this, "selnote");
00269 Q_ASSERT(s);
00270 if(s->exec()==QDialog::Accepted)
00271 createLink(s->url(), url());
00272 }
00273
00274 void File::deleteNote() {
00275 deleteLink(fileform->links->url(), url());
00276 }
00277
00278 void File::createNote() {
00279 newNote(url());
00280 }
00281
00282 void File::addToProject() {
00283 SelectItem *s=new SelectItem(this, "selproject");
00284 Q_ASSERT(s);
00285 s->setData("project/project", "select no, title from project order by title");
00286 if(s->exec()==QDialog::Accepted) {
00287 QSqlQuery q("insert into project_bibliography (project_no, tab_no, id_no) values ("+
00288 no(s->url())+",tabid('partdata'), "+no(url())+")");
00289 }
00290 }
00291
00292 void File::nextFile() {
00293 QSqlQuery query("select no from partdata where filename>'"+formcursor.value("filename").toString()+"' and part_no="+formcursor.value("part_no").toString()+" order by filename");
00294 if(query.first()) {
00295 qApp->postEvent(topLevelWidget(), new UrlEvent(LitUrl("lit://file/partdata?no="+query.value(0).toString()), false));
00296 }
00297 }
00298
00299 void File::prevFile() {
00300 QSqlQuery query("select no from partdata where filename<'"+formcursor.value("filename").toString()+"' and part_no="+formcursor.value("part_no").toString()+" order by filename desc");
00301 if(query.first()) {
00302 qApp->postEvent(topLevelWidget(), new UrlEvent(LitUrl("lit://file/partdata?no="+query.value(0).toString()), false));
00303 }
00304 }
00305
00306 void File::showPart() {
00307 qApp->postEvent(topLevelWidget(), new UrlEvent(LitUrl("lit://part/part?no="+formcursor.value("part_no").toString()), false));
00308 }
00309
00310 #ifdef HAS_KDE
00311 void File::runApp() {
00312 RunDialog r(this);
00313 r.setData("file://"+file);
00314 r.exec();
00315 }
00316 #endif