]> ruin.nu Git - adress.git/blob - mainwindow.cpp
Have fixed the saving..
[adress.git] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include <qfile.h>
3 #include <qfiledialog.h>
4 #include <qtextstream.h>
5 #include <qstring.h>
6 #include <qlistview.h>
7 #include "contact.h"
8 #include <iostream>
9
10 /* BRÖÖL
11  *  Constructs a MainWindow which is a child of 'parent', with the 
12  *  name 'name' and widget flags set to 'f' 
13  */
14 MainWindow::MainWindow( QWidget* parent,  const char* name, WFlags fl )
15     : CMainWindowBase( parent, name, fl )
16 {
17         slotLoad();
18 }
19
20 /*  
21  *  Destroys the object and frees any allocated resources
22  */
23 MainWindow::~MainWindow()
24 {
25     // no need to delete child widgets, Qt does it all for us
26 }
27
28 /* 
29  * public slot
30  */
31 void MainWindow::slotSave()
32 {
33         if (filename == "")
34                 return;
35
36         QFile f(filename);
37
38         if ( !f.open( IO_WriteOnly ) )
39                     return false;
40               
41         QTextStream t(&f);
42         QString s;
43         CContact* contact = CContact::getFirst();
44
45         while ( contact != '\0')
46         {
47                 t << contact->firstname() << "\n";
48                 t << contact->lastname() << "\n";
49                 t << contact->adress() << "\n";
50                 t << contact->phoneNumber() << "\n";
51                 contact=contact->getNext();
52         }
53         f.close();
54 }
55 /* 
56  * public slot
57  */
58 void MainWindow::slotAdd()
59 {
60     qWarning( "MainWindow::slotAdd() not yet implemented!" ); 
61 }
62 /* 
63  * public slot
64  */
65 void MainWindow::slotDel()
66 {
67     qWarning( "MainWindow::slotDel() not yet implemented!" ); 
68 }
69 /* 
70  * public slot
71  */
72 void MainWindow::slotLoad()
73 {
74         slotSave();
75         QFile f(filename);
76         do
77         {
78                 filename(QFileDialog::getOpenFileName(0, "Adressfiler (*.adr)", this)); 
79         }
80         while ( !f.open( IO_ReadOnly ) );
81                         
82         QTextStream t(&f);
83
84         while (!t.eof())
85         {
86                 QString fname = t.readLine();
87                 QString lname = t.readLine();
88                 QString adr             = t.readLine();
89                 QString pn              = t.readLine();
90     if (fname == "" && lname == "" && lname == "" && pn == "")
91                               continue;
92                 (void) new CContact(fname, lname, adr, pn);
93         }
94         updateView();
95 }
96 /* 
97  * public slot
98  */
99 void MainWindow::slotSearch()
100 {
101     qWarning( "MainWindow::slotSearch() not yet implemented!" ); 
102 }
103
104 void slotModify(QListViewItem* lvi)
105 {
106     qWarning( "MainWindow::slotModif(QListViewItem*) not yet implemented!" ); 
107 }
108 /*
109  * This funtion puts all contacts in the listview
110  */
111 void MainWindow::updateView()
112 {
113         
114         ContactView->clear();
115
116         CContact* contact = CContact::getFirst();
117
118         int i = 1;
119         char s[100];
120
121         while (contact != '\0')
122         {
123                 sprintf(s,"%i",i);//.arg(i);
124                 (void) new QListViewItem(ContactView, s, contact->lastname(), contact->firstname(), contact->adress(), contact->phoneNumber());
125                 contact = contact->getNext();
126                 i++;
127         }
128 }