summaryrefslogtreecommitdiff
path: root/core/pim/notes/mainwindow.cpp
Unidiff
Diffstat (limited to 'core/pim/notes/mainwindow.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/pim/notes/mainwindow.cpp255
1 files changed, 255 insertions, 0 deletions
diff --git a/core/pim/notes/mainwindow.cpp b/core/pim/notes/mainwindow.cpp
new file mode 100644
index 0000000..d10578a
--- a/dev/null
+++ b/core/pim/notes/mainwindow.cpp
@@ -0,0 +1,255 @@
1/*
2 * OPIE Notes
3 *
4 * based on NoteZ 1.1.0 by Henning Holtschneider <hh@holtschneider.com>
5 *
6 * moved to OPIE Pim framework by Marcin Juszkiewicz <openembedded@hrw.one.pl>
7 *
8 * History:
9 * - 2005.10.12 - started work
10 * - 2005.10.19 - version 0.2:
11 * - first working version
12 * - info sent to NoteZ author and to OPIE devel
13 *
14 * - 2005.10.20 - version 0.3:
15 * - load/save in UTF-8
16 * - load all files from Documents/text/plain
17 * - create .txt files not .ntz - timestamp used as name
18 * - one variable (documentsDirName) keep location of notes
19 * - code (re)indented and converted to spaces
20 * - variables translated to English (were in German)
21 * - started work on beaming
22 *
23 * ToDo:
24 * - beaming
25 * - moving to SQLite database
26 * - category support
27 * - searching
28 *
29 */
30
31#include "mainwindow.h"
32#include "editwindow.h"
33#include <qpe/ir.h>
34
35mainWindowWidget::mainWindowWidget( QWidget *parent, const char *name, WFlags)
36 : Opie::OPimMainWindow( "Notes", "Notes", tr( "Note" ), "notes",
37 parent, name, WType_TopLevel | WStyle_ContextHelp )
38{
39 setCaption( tr("Notes"));
40 notesList = new QListBox(this, "notesList");
41 setCentralWidget(notesList);
42
43 documentsDirName = QPEApplication::documentDir() + "/text/plain/";
44 this->selected = -1;
45 refreshList();
46
47 QObject::connect(notesList, SIGNAL(returnPressed(QListBoxItem*)), this, SLOT(slotItemEdit()));
48 QObject::connect(notesList, SIGNAL(doubleClicked(QListBoxItem*)), this, SLOT(slotItemEdit()));
49}
50
51void mainWindowWidget::deleteFile()
52{
53 if( notesList->count() > 0 )
54 {
55 switch (QMessageBox::warning(0, tr("Delete note"), tr("Really delete\n'") + notesList->currentText() + "'?",
56 QMessageBox::Yes, QMessageBox::No))
57 {
58 case QMessageBox::Yes:
59 this->selected = notesList->currentItem();
60 QFile::remove(documentsDirName + fileList[notesList->currentItem()]);
61 refreshList();
62 break;
63
64 case QMessageBox::No:
65 // don't delete
66 break;
67 }
68 }
69}
70
71void mainWindowWidget::editFile(QString filename, int create)
72{
73 editWindowWidget *editWindow = new editWindowWidget(0, "editWindow", true);
74
75 editWindow->loadFile(filename);
76
77 if(QPEApplication::execDialog(editWindow) == QDialog::Accepted)
78 {
79 editWindow->saveFile(filename);
80 if( create )
81 {
82 // the new selection will be always at the end and count is already
83 // 1 bigger than selected
84 this->selected = notesList->count();
85 }
86 }
87
88 refreshList();
89}
90
91int mainWindowWidget::create()
92{
93 QString name;
94 int now = time(0);
95
96 this->editFile(name.sprintf(documentsDirName + "%d.txt", now), true );
97
98 return 0; //FIXME
99}
100
101void mainWindowWidget::slotItemEdit()
102{
103 openFile();
104}
105
106void mainWindowWidget::slotItemDelete()
107{
108 deleteFile();
109}
110
111void mainWindowWidget::slotItemNew()
112{
113 create();
114}
115
116void mainWindowWidget::slotItemDuplicate()
117{
118 QString fileName = documentsDirName + fileList[notesList->currentItem()];
119 int now = time(0);
120
121 QFile fileOld(fileName);
122
123 if (fileOld.exists())
124 {
125 if (!fileOld.open(IO_ReadOnly))
126 {
127 QMessageBox::warning(0, tr("File i/o error"), fileName.sprintf(tr("Could not read file '%s'"), fileName));
128 }
129 else
130 {
131 QFile fileNew(documentsDirName + fileName.sprintf("%d.txt", now));
132
133 if (!fileNew.exists())
134 {
135 if(fileNew.open(IO_WriteOnly))
136 {
137 QTextStream inStream(&fileOld);
138 inStream.setEncoding(QTextStream::UnicodeUTF8);
139
140 QTextStream outStream(&fileNew);
141 outStream.setEncoding(QTextStream::UnicodeUTF8);
142 outStream << inStream.read();
143
144 fileOld.close();
145 fileNew.close();
146 refreshList();
147 }
148 }
149 }
150 }
151}
152
153void mainWindowWidget::openFile()
154{
155 int number = notesList->currentItem();
156
157 if( notesList->count() > 0 )
158 {
159 this->selected = number;
160 this->editFile(documentsDirName + fileList[number], false);
161 }
162}
163
164void mainWindowWidget::refreshList()
165{
166 unsigned int item;
167 QString title;
168
169 notesList->clear();
170
171 fileList.setPath(documentsDirName);
172 fileList.setFilter(QDir::Files);
173 fileList.setSorting(QDir::Name);
174
175 for (item = 0; item < fileList.count(); item++)
176 {
177 QFile file(documentsDirName + fileList[item]);
178
179 if (!file.open(IO_ReadOnly))
180 {
181 QMessageBox::warning(0, tr("File i/o error"), title.sprintf(tr("Could not read file '%s'"), fileList[item]));
182 }
183 else
184 {
185 QTextStream inStream(&file);
186 inStream.setEncoding(QTextStream::UnicodeUTF8);
187
188 if (!inStream.atEnd())
189 {
190 title = inStream.readLine();
191 }
192 else
193 {
194 title = tr("<empty file>");
195 }
196
197 if (title.length() < 1)
198 {
199 title = tr("<no caption>");
200 }
201
202 file.close();
203
204 notesList->insertItem(title);
205 }
206 }
207
208 if( notesList->count() > 0 )
209 {
210 if( this->selected == -1 )
211 {
212 notesList->setCurrentItem( 0 );
213 }
214 else
215 {
216 if( notesList->count() > this->selected )
217 {
218 notesList->setCurrentItem( this->selected );
219 }
220 else
221 {
222 notesList->setCurrentItem( notesList->count() - 1 );
223 }
224
225 }
226 }
227 else
228 {
229 this->selected = -1;
230 }
231
232}
233
234void mainWindowWidget::slotItemBeam()
235{
236 Ir obex;
237
238 obex.send(documentsDirName + fileList[notesList->currentItem()], "test", "text/plain");
239}
240
241void mainWindowWidget::slotItemFind() { toBeDone();}
242void mainWindowWidget::slotConfigure() { toBeDone();}
243void mainWindowWidget::flush() { toBeDone();}
244void mainWindowWidget::reload() { toBeDone();}
245bool mainWindowWidget::remove( int /*uid*/ ) { toBeDone(); return false; }
246void mainWindowWidget::beam( int /*uid*/ ) { toBeDone();}
247void mainWindowWidget::show( int /*uid*/ ) { toBeDone();}
248void mainWindowWidget::edit( int /*uid*/ ) { toBeDone();}
249void mainWindowWidget::add( const Opie::OPimRecord& ) { toBeDone();}
250
251void mainWindowWidget::toBeDone(void)
252{
253 QMessageBox::information( this, "Notes", tr("Not yet implemented"));
254}
255