00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "mainwindow.h"
00035 #include "editwindow.h"
00036 #include <qpe/ir.h>
00037
00038 mainWindowWidget::mainWindowWidget( QWidget *parent, const char *name, WFlags)
00039 : Opie::OPimMainWindow( "Notes", "Notes", tr( "Note" ), "notes",
00040 parent, name, WType_TopLevel | WStyle_ContextHelp )
00041 {
00042 setCaption( tr("Notes"));
00043 notesList = new QListBox(this, "notesList");
00044 setCentralWidget(notesList);
00045
00046 documentsDirName = QPEApplication::documentDir() + "/text/plain/";
00047
00048 fileList.setPath(documentsDirName);
00049
00050 if(!fileList.exists())
00051 {
00052 fileList.setPath(QPEApplication::documentDir() + "/text/");
00053
00054 if(!fileList.exists())
00055 {
00056 QString text;
00057
00058 if(!fileList.mkdir(fileList.absPath()))
00059 {
00060 QMessageBox::critical(0, tr("i/o error"), text.sprintf(tr("Could not create directory '%s'"), fileList.absPath()));
00061 }
00062 else
00063 {
00064 fileList.setPath(documentsDirName);
00065
00066 if(!fileList.mkdir(fileList.absPath()))
00067 {
00068 QMessageBox::critical(0, tr("i/o error"), text.sprintf(tr("Could not create directory '%s'"), fileList.absPath()));
00069 }
00070 }
00071 }
00072 }
00073
00074 this->selected = -1;
00075 refreshList();
00076
00077 QObject::connect(notesList, SIGNAL(returnPressed(QListBoxItem*)), this, SLOT(slotItemEdit()));
00078 QObject::connect(notesList, SIGNAL(doubleClicked(QListBoxItem*)), this, SLOT(slotItemEdit()));
00079 }
00080
00081 void mainWindowWidget::deleteFile()
00082 {
00083 if( notesList->count() > 0 )
00084 {
00085 switch (QMessageBox::warning(0, tr("Delete note"), tr("Really delete\n'") + notesList->currentText() + "'?",
00086 QMessageBox::Yes, QMessageBox::No))
00087 {
00088 case QMessageBox::Yes:
00089 this->selected = notesList->currentItem();
00090 QFile::remove(documentsDirName + fileList[notesList->currentItem()]);
00091 refreshList();
00092 break;
00093
00094 case QMessageBox::No:
00095
00096 break;
00097 }
00098 }
00099 }
00100
00101 void mainWindowWidget::editFile(QString filename, int create)
00102 {
00103 editWindowWidget *editWindow = new editWindowWidget(0, "editWindow", true);
00104
00105 editWindow->loadFile(filename);
00106
00107 if(QPEApplication::execDialog(editWindow) == QDialog::Accepted)
00108 {
00109 editWindow->saveFile(filename);
00110 if( create )
00111 {
00112
00113
00114 this->selected = notesList->count();
00115 }
00116 }
00117
00118 refreshList();
00119 }
00120
00121 int mainWindowWidget::create()
00122 {
00123 QString name;
00124 int now = time(0);
00125
00126 this->editFile(name.sprintf(documentsDirName + "%d.txt", now), true );
00127
00128 return 0;
00129 }
00130
00131 void mainWindowWidget::slotItemEdit()
00132 {
00133 openFile();
00134 }
00135
00136 void mainWindowWidget::slotItemDelete()
00137 {
00138 deleteFile();
00139 }
00140
00141 void mainWindowWidget::slotItemNew()
00142 {
00143 create();
00144 }
00145
00146 void mainWindowWidget::slotItemDuplicate()
00147 {
00148 QString fileName = documentsDirName + fileList[notesList->currentItem()];
00149 int now = time(0);
00150
00151 QFile fileOld(fileName);
00152
00153 if (fileOld.exists())
00154 {
00155 if (!fileOld.open(IO_ReadOnly))
00156 {
00157 QMessageBox::warning(0, tr("File i/o error"), fileName.sprintf(tr("Could not read file '%s'"), fileName));
00158 }
00159 else
00160 {
00161 QFile fileNew(documentsDirName + fileName.sprintf("%d.txt", now));
00162
00163 if (!fileNew.exists())
00164 {
00165 if(fileNew.open(IO_WriteOnly))
00166 {
00167 QTextStream inStream(&fileOld);
00168 inStream.setEncoding(QTextStream::UnicodeUTF8);
00169
00170 QTextStream outStream(&fileNew);
00171 outStream.setEncoding(QTextStream::UnicodeUTF8);
00172 outStream << inStream.read();
00173
00174 fileOld.close();
00175 fileNew.close();
00176 refreshList();
00177 }
00178 }
00179 }
00180 }
00181 }
00182
00183 void mainWindowWidget::openFile()
00184 {
00185 int number = notesList->currentItem();
00186
00187 if( notesList->count() > 0 )
00188 {
00189 this->selected = number;
00190 this->editFile(documentsDirName + fileList[number], false);
00191 }
00192 }
00193
00194 void mainWindowWidget::refreshList()
00195 {
00196 unsigned int item;
00197 QString title;
00198
00199 notesList->clear();
00200
00201 fileList.setPath(documentsDirName);
00202 fileList.setFilter(QDir::Files);
00203 fileList.setSorting(QDir::Name);
00204
00205 for (item = 0; item < fileList.count(); item++)
00206 {
00207 QFile file(documentsDirName + fileList[item]);
00208
00209 if (!file.open(IO_ReadOnly))
00210 {
00211 QMessageBox::warning(0, tr("File i/o error"), title.sprintf(tr("Could not read file '%s'"), fileList[item]));
00212 }
00213 else
00214 {
00215 QTextStream inStream(&file);
00216 inStream.setEncoding(QTextStream::UnicodeUTF8);
00217
00218 if (!inStream.atEnd())
00219 {
00220 title = inStream.readLine();
00221 }
00222 else
00223 {
00224 title = tr("<empty file>");
00225 }
00226
00227 if (title.length() < 1)
00228 {
00229 title = tr("<no caption>");
00230 }
00231
00232 file.close();
00233
00234 notesList->insertItem(title);
00235 }
00236 }
00237
00238 if( notesList->count() > 0 )
00239 {
00240 if( this->selected == -1 )
00241 {
00242 notesList->setCurrentItem( 0 );
00243 }
00244 else
00245 {
00246 if( notesList->count() > this->selected )
00247 {
00248 notesList->setCurrentItem( this->selected );
00249 }
00250 else
00251 {
00252 notesList->setCurrentItem( notesList->count() - 1 );
00253 }
00254
00255 }
00256 }
00257 else
00258 {
00259 this->selected = -1;
00260 }
00261
00262 }
00263
00264 void mainWindowWidget::slotItemBeam()
00265 {
00266 Ir obex;
00267
00268 obex.send(documentsDirName + fileList[notesList->currentItem()], "test", "text/plain");
00269 }
00270
00271 void mainWindowWidget::slotItemFind() { toBeDone();}
00272 void mainWindowWidget::slotConfigure() { toBeDone();}
00273 void mainWindowWidget::flush() { toBeDone();}
00274 void mainWindowWidget::reload() { toBeDone();}
00275 bool mainWindowWidget::remove( int ) { toBeDone(); return false; }
00276 void mainWindowWidget::beam( int ) { toBeDone();}
00277 void mainWindowWidget::show( int ) { toBeDone();}
00278 void mainWindowWidget::edit( int ) { toBeDone();}
00279 void mainWindowWidget::add( const Opie::OPimRecord& ) { toBeDone();}
00280
00281 void mainWindowWidget::toBeDone(void)
00282 {
00283 QMessageBox::information( this, "Notes", tr("Not yet implemented"));
00284 }
00285