Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

notes.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2002 L.J. Potter <llornkcor@handhelds.org>
00003 
00004 **  All rights reserved.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 **********************************************************************/
00015 
00016 #include "notes.h"
00017 
00018 /* OPIE */
00019 #include <opie2/odebug.h>
00020 #include <opie2/otaskbarapplet.h>
00021 #include <opie2/oresource.h>
00022 #include <qpe/filemanager.h>
00023 #include <qpe/qpeapplication.h>
00024 #include <qpe/timestring.h>
00025 #include <qpe/applnk.h>
00026 #include <qpe/ir.h>
00027 #include <qpe/config.h>
00028 using namespace Opie::Core;
00029 using namespace Opie::Ui;
00030 
00031 /* QT */
00032 #include <qmultilineedit.h>
00033 #include <qlistbox.h>
00034 #include <qpopupmenu.h>
00035 #include <qmessagebox.h>
00036 #include <qapplication.h>
00037 #include <qdir.h>
00038 #include <qfile.h>
00039 #include <qpoint.h>
00040 #include <qpushbutton.h>
00041 #include <qpainter.h>
00042 #include <qlayout.h>
00043 #include <qframe.h>
00044 #include <qpixmap.h>
00045 #include <qstring.h>
00046 #include <qstringlist.h>
00047 #include <qtimer.h>
00048 
00049 /* STD */
00050 #include <stdlib.h>
00051 
00052 NotesControl::NotesControl( QWidget *, const char * )
00053         : QVBox( 0, "NotesControl",/* WDestructiveClose | */WStyle_StaysOnTop )
00054 //        : QFrame( parent, name, WDestructiveClose | WStyle_StaysOnTop | WType_Popup )
00055 {
00056     QDir d( QDir::homeDirPath()+"/notes");
00057     if( !d.exists()) {
00058         odebug << "make dir" << oendl;
00059         if(!d.mkdir( QDir::homeDirPath()+"/notes", true))
00060             odebug << "<<<<<<<<<<<<<<<<<<<<<<<<<<<make dir failed" << oendl;
00061     }
00062     Config cfg("Notes");
00063      cfg.setGroup("Options");
00064      showMax = cfg.readBoolEntry("ShowMax", false);
00065 
00066      setFrameStyle( QFrame::PopupPanel | QFrame::Raised );
00067     loaded=false;
00068     edited=false;
00069     doPopulate=true;
00070     isNew=false;
00071     QVBox *vbox = new QVBox( this, "Vlayout" );
00072     QHBox  *hbox = new QHBox( this, "HLayout" );
00073 
00074     view = new QMultiLineEdit(vbox, "OpieNotesView");
00075 
00076     box = new QListBox(vbox, "OpieNotesBox");
00077 
00078     QPEApplication::setStylusOperation( box->viewport(),QPEApplication::RightOnHold);
00079 
00080     box->setFixedHeight(50);
00081 
00082     vbox->setMargin( 6 );
00083     vbox->setSpacing( 3 );
00084 
00085 
00086 
00087     setFocusPolicy(QWidget::StrongFocus);
00088 
00089     newButton= new QPushButton( hbox, "newButton" );
00090     newButton->setText(tr("New"));
00091 
00092 
00093     saveButton= new QPushButton( hbox, "saveButton" );
00094     saveButton->setText(tr("Save"));
00095 
00096 
00097     deleteButton= new QPushButton( hbox, "deleteButton" );
00098     deleteButton->setText(tr("Delete"));
00099 
00100 
00101 
00102     connect( box, SIGNAL( mouseButtonPressed(int,QListBoxItem*,const QPoint&)),
00103              this,SLOT( boxPressed(int,QListBoxItem*,const QPoint&)) );
00104 
00105     connect(box, SIGNAL(highlighted(const QString&)), this, SLOT(slotBoxSelected(const QString&)));
00106 
00107     connect( &menuTimer, SIGNAL( timeout() ), SLOT( showMenu() ) );
00108 
00109     connect(view,SIGNAL( textChanged() ), this, SLOT(slotViewEdited() ) );
00110 
00111     connect(newButton, SIGNAL(clicked()), this, SLOT(slotNewButton()));
00112     connect(saveButton, SIGNAL(clicked()), this, SLOT(slotSaveButton()));
00113     connect(deleteButton, SIGNAL(clicked()), this, SLOT(slotDeleteButtonClicked()));
00114 
00115     populateBox();
00116     load();
00117     setCaption("Notes");
00118       //  parent->setFocus();
00119 }
00120 
00121 void NotesControl::slotSaveButton() {
00122     slotNewButton();
00123     populateBox();
00124 }
00125 
00126 void NotesControl::slotDeleteButtonClicked() {
00127     switch ( QMessageBox::warning(this,tr("Delete?")
00128                                   ,tr("Do you really want to<BR><B> delete</B> this note ?")
00129                                   ,tr("Yes"),tr("No"),0,1,1) ) {
00130       case 0:
00131           slotDeleteButton();
00132           break;
00133     };
00134 }
00135 
00136 void NotesControl::slotDeleteButton() {
00137 
00138     QString selectedText = box->currentText();
00139     odebug << "deleting "+selectedText << oendl;
00140 
00141     if( !selectedText.isEmpty()) {
00142 
00143         Config cfg("Notes");
00144         cfg.setGroup("Docs");
00145         int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
00146         QString entryName, entryName2;;
00147         for ( int i = 0; i < noOfFiles; i++ ) {
00148             entryName.sprintf( "File%i", i + 1 );
00149             if(selectedText == cfg.readEntry( entryName )) {
00150                 odebug << "removing " << selectedText.latin1() << ", " << i << "" << oendl;
00151                 for ( int j = i; j < noOfFiles; j++ ) {
00152                     entryName.sprintf( "File%i", i + 1  );
00153                     entryName2.sprintf( "File%i", i + 2 );
00154                     QString temp = cfg.readEntry(entryName2);
00155                     odebug << "move "+temp << oendl;
00156                     cfg.writeEntry(entryName, temp);
00157                     i++;
00158                 }
00159                 cfg.writeEntry("NumberOfFiles", noOfFiles-1 );
00160                 entryName.sprintf( "File%i", noOfFiles );
00161                 cfg.removeEntry(entryName);
00162                 cfg.write();
00163                 DocLnk nf(selectedText);
00164                 nf.removeFiles();
00165                 QString fi=QPEApplication::documentDir()+"/text/plain/"+selectedText+".desktop";
00166                 odebug << fi << oendl;
00167 
00168                 QFile f( fi);
00169                 if( !f.remove()) odebug << ".desktop file not removed" << oendl;
00170 
00171             }
00172         }
00173         view->clear();
00174 
00175         populateBox();
00176     }
00177 }
00178 
00179 void NotesControl::slotNewButton() {
00180     if(edited) save();
00181     view->clear();
00182     view->setFocus();
00183     edited=false;
00184     isNew=false;
00185 }
00186 
00187 void NotesControl::slotBeamButton() {
00188   Ir ir;
00189   if(!ir.supported()){
00190   } else {
00191       this->hide();
00192     QString selectedText = box->currentText();
00193     if( !selectedText.isEmpty()) {
00194         QString file = QDir::homeDirPath()+"/"+selectedText;
00195         QFile f(file);
00196            Ir *irFile = new Ir(this, "IR");
00197           connect( irFile, SIGNAL(done(Ir*)), this, SLOT( slotBeamFinished(Ir*)));
00198           irFile->send( file, "Note", "text/plain" );
00199     }
00200   }
00201 }
00202 
00203 void NotesControl::slotBeamFinished(Ir *) {
00204     this->show();
00205 }
00206 
00207 void NotesControl::boxPressed(int mouse, QListBoxItem *, const QPoint&) {
00208   switch (mouse) {
00209     case 1:{
00210     }
00211     break;
00212   case 2:
00213     menuTimer.start( 500, TRUE );
00214     break;
00215   };
00216 }
00217 
00218 void NotesControl::slotBoxSelected(const QString &itemString) {
00219     if(edited) {
00220         save();
00221     }
00222     loaded=false;
00223     edited=false;
00224     load(itemString);
00225 }
00226 
00227 
00228 void NotesControl::showMenu() {
00229   QPopupMenu *m = new QPopupMenu(0);
00230   m->insertItem(  tr( "Beam Out" ), this,  SLOT( slotBeamButton() ));
00231   m->insertItem(  tr( "Search For..." ), this,  SLOT( slotSearch() ));
00232   m->insertItem(  tr( "Toggle Maximized" ), this,  SLOT( slotShowMax() ));
00233   m->insertSeparator();
00234   m->insertItem(  tr( "Delete" ), this,  SLOT( slotDeleteButton() ));
00235   m->setFocus();
00236   m->exec( QCursor::pos() );
00237 
00238   if(m) delete m;
00239 }
00240 
00241 void NotesControl::focusOutEvent ( QFocusEvent * e) {
00242     if( e->reason() == QFocusEvent::Popup)
00243         save();
00244     else {
00245         if(!loaded) {
00246         populateBox();
00247         load();
00248         }
00249     }
00250     QWidget::focusOutEvent(e);
00251 }
00252 
00253 void NotesControl::save() {
00254     Config cfg("Notes");
00255     cfg.setGroup("Docs");
00256     if( edited) {
00257 //        odebug << "is edited" << oendl;
00258         QString rt = view->text();
00259         if( rt.length()>1) {
00260             QString pt = rt.simplifyWhiteSpace();
00261             int i = pt.find( ' ', pt.find( ' ' )+2 );
00262             QString docname = pt;
00263             if ( i > 0 )
00264                 docname = pt.left(i);
00265               // remove "." at the beginning
00266             while( docname.startsWith( "." ) )
00267                 docname = docname.mid( 1 );
00268             docname.replace( QRegExp("/"), "_" );
00269               // cut the length. filenames longer than that don't make sense
00270               // and something goes wrong when they get too long.
00271             if ( docname.length() > 40 )
00272                 docname = docname.left(40);
00273             if ( docname.isEmpty() )
00274                 docname = "Empty Text";
00275 //            odebug << docname << oendl;
00276 
00277             if( oldDocName != docname) {
00278                 int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
00279                 QString entryName;
00280                 entryName.sprintf( "File%i", noOfFiles + 1 );
00281                 cfg.writeEntry( entryName,docname );
00282                 cfg.writeEntry("NumberOfFiles", noOfFiles+1 );
00283                 cfg.write();
00284             }
00285 //             else
00286 //                 odebug << "oldname equals docname" << oendl;
00287 
00288             doc = new DocLnk(docname);
00289             if(QFile(doc->linkFile()).exists())
00290                odebug << "puppie" << oendl;
00291             doc->setType("text/plain");
00292             doc->setName(docname);
00293             QString temp = docname.replace( QRegExp(" "), "_" );
00294             doc->setFile( QDir::homeDirPath()+"/notes/"+temp);
00295             FileManager fm;
00296             if ( !fm.saveFile( *doc, rt ) ) {
00297             }
00298 
00299             oldDocName=docname;
00300             edited=false;
00301 //            odebug << "save" << oendl;
00302             if (doPopulate)
00303                 populateBox();
00304         }
00305     cfg.writeEntry( "LastDoc",oldDocName );
00306     cfg.write();
00307 
00308     }
00309 }
00310 
00311 void NotesControl::populateBox() {
00312     box->clear();
00313 //    odebug << "populate" << oendl;
00314     Config cfg("Notes");
00315     cfg.setGroup("Docs");
00316     int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
00317     QStringList list;
00318     QString entryName;
00319     for ( int i = 0; i < noOfFiles; i++ ) {
00320         entryName.sprintf( "File%i", i + 1 );
00321         list.append(cfg.readEntry( entryName ));
00322     }
00323     list.sort();
00324     box->insertStringList(list,-1);
00325     doPopulate=false;
00326     update();
00327 }
00328 
00329 void NotesControl::load() {
00330 
00331     if(!loaded) {
00332         Config cfg("Notes");
00333         cfg.setGroup("Docs");
00334         QString lastDoc=cfg.readEntry( "LastDoc","notes");
00335         DocLnk nf;
00336         nf.setType("text/plain");
00337         nf.setFile(lastDoc);
00338 
00339         loadDoc(nf);
00340         loaded=true;
00341         oldDocName=lastDoc;
00342         cfg.writeEntry( "LastDoc",oldDocName );
00343         cfg.write();
00344     }
00345 }
00346 
00347 void NotesControl::load(const QString & file) {
00348     odebug << "loading "+file << oendl;
00349     QString name = file;
00350     QString temp;
00351     if( !QFile( QDir::homeDirPath()+"/"+file).exists() )
00352         temp =  QDir::homeDirPath()+"/notes/"+ name.replace( QRegExp(" "), "_" );
00353     else
00354         temp = name;
00355             if(!loaded) {
00356         DocLnk nf;
00357         nf.setType("text/plain");
00358         nf.setFile( temp);
00359         if(!temp.isEmpty())
00360         loadDoc(nf);
00361         loaded=true;
00362     }
00363 //    view->setFocus();
00364     oldDocName=file;
00365     Config cfg("Notes");
00366     cfg.setGroup("Docs");
00367     cfg.writeEntry( "LastDoc",oldDocName );
00368     cfg.write();
00369 }
00370 
00371 void NotesControl::loadDoc( const DocLnk &f) {
00372     FileManager fm;
00373     QString txt;
00374     if ( !fm.loadFile( f, txt ) ) {
00375         odebug << "could not load file "+f.file() << oendl;
00376         return;
00377     }
00378     view->setText(txt);
00379 }
00380 
00381 void NotesControl::slotViewEdited() {
00382      if(loaded) {
00383          edited=true;
00384      }
00385 }
00386 
00387 
00388 void NotesControl::slotShowMax() {
00389      Config cfg("Notes");
00390      cfg.setGroup("Options");
00391      showMax=!showMax;
00392      cfg.writeEntry("ShowMax", showMax);
00393      cfg.write();
00394      hide();
00395 }
00396 
00397 void NotesControl::slotSearch() {
00398     int boxCount = box->count();
00399     for(int i=0;i< boxCount;i++) {
00400 
00401     }
00402 }
00403 
00404 // void NotesControl::keyReleaseEvent( QKeyEvent *e) {
00405 
00406 //     switch ( e->state() ) {
00407 //           case ControlButton:
00408 //               if(e->key() == Key_C) { //copy
00409 //                   odebug << "copy" << oendl;
00410 //     QClipboard *cb = QApplication::clipboard();
00411 //     QString text;
00412 
00413 //     // Copy text from the clipboard (paste)
00414 //     text = cb->text();
00415 //               }
00416 //               if(e->key() == Key_X) { //cut
00417 //               }
00418 //               if(e->key() == Key_V) { //paste
00419 //     QClipboard *cb = QApplication::clipboard();
00420 //     QString text;
00421 // //view
00422 //     cb->setText();
00423 //               }
00424 //               break;
00425 //         };
00426 // QWidget::keyReleaseEvent(e);
00427 // }
00428 
00429 //===========================================================================
00430 
00431 NotesApplet::NotesApplet( QWidget *parent, const char *name )
00432         : QWidget( parent, name ) {
00433     setFixedHeight( AppLnk::smallIconSize() );
00434     setFixedWidth( AppLnk::smallIconSize() );
00435     notesPixmap = Opie::Core::OResource::loadImage( "edit", Opie::Core::OResource::SmallIcon );
00436    vc = new NotesControl;
00437 }
00438 
00439 NotesApplet::~NotesApplet() {
00440     delete vc;
00441 }
00442 
00443 int NotesApplet::position()
00444 {
00445     return 6;
00446 }
00447 
00448 void NotesApplet::mousePressEvent( QMouseEvent *) {
00449     if( !vc->isHidden()) {
00450         vc->doPopulate=false;
00451         vc->save();
00452         vc->close();
00453     } else {
00454 //    vc = new NotesControl;
00455 //    QPoint curPos = mapToGlobal( rect().topLeft() );
00456          if(vc->showMax) {
00457              odebug << "show max" << oendl;
00458              vc->showMaximized();
00459          } else {
00460              odebug << "no show max" << oendl;
00461              QWidget *wid = QPEApplication::desktop();
00462             QRect rect = QApplication::desktop()->geometry();
00463              vc->setGeometry( ( wid->width() / 2) - ( vc->width() / 2 ) , 28 , wid->width() -10 , 180);
00464             vc->move ( (rect.center()/2) - (vc->rect().center()/2));
00465 //             vc->move(  (( wid->width() / 2) - ( vc->width() / 2 ))-4, 28);
00466         }
00467         vc->show();
00468         vc->doPopulate=true;
00469         vc->populateBox();
00470         vc->doPopulate=false;
00471         vc->loaded=false;
00472 
00473         vc->load();
00474 //        this->setFocus();
00475         vc->view->setFocus();
00476     }
00477 }
00478 
00479 void NotesApplet::paintEvent( QPaintEvent* ) {
00480     QPainter p(this);
00481     p.drawPixmap( 0, 2, notesPixmap );
00482 }
00483 
00484 
00485 EXPORT_OPIE_APPLET_v1(  NotesApplet )
00486 

Generated on Sat Nov 5 16:16:44 2005 for OPIE by  doxygen 1.4.2