]> ruin.nu Git - adress.git/blob - mainwindow.cpp
094e54f9156730600c168633283073fa350c3d55
[adress.git] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include <iostream>
3
4 #include "contactmodifyer.h"
5
6 /* BRÖÖL
7  *  Constructs a MainWindow which is a child of 'parent', with the 
8  *  name 'name' and widget flags set to 'f' 
9  */
10 MainWindow::MainWindow( QWidget* parent,  const char* name, WFlags fl )
11     : CMainWindowBase( parent, name, fl )
12 {
13 }
14
15 /*  
16  *  Destroys the object and frees any allocated resources
17  */
18 MainWindow::~MainWindow()
19 {
20     // no need to delete child widgets, Qt does it all for us
21 }
22
23 /* 
24  * public slot
25  */
26 void MainWindow::slotSave()
27 {
28         if (filename.isNull())
29         {
30                 filename = QFileDialog::getSaveFileName( 0, "Adresslista (*.adr)");
31                 if (filename.isNull())
32                 {
33                         return;
34                 }
35         }                     
36
37         QFile f(filename);
38
39         if ( !f.open( IO_WriteOnly ) )
40                     return;
41               
42         QTextStream t(&f);
43         QString s;
44         CContact* contact = CContact::getFirst();
45
46         while ( contact != '\0')
47         {
48                 t << contact->firstname() << "\n";
49                 t << contact->lastname() << "\n";
50                 t << contact->adress() << "\n";
51                 t << contact->phoneNumber() << "\n";
52                 contact=contact->getNext();
53         }
54         f.close();
55 }
56 /* 
57  * public slot
58  */
59 void MainWindow::slotAdd()
60 {
61         ContactModifier add;
62         if(add.exec() == QDialog::Accepted)
63                 updateView();
64 }
65 /* 
66  * public slot
67  */
68 void MainWindow::slotDel()
69 {
70         QListViewItem* lvi = ContactView->selectedItem();
71
72   if (lvi == '\0')
73         {
74                 return;
75         }
76         
77         int num = lvi->text(0).toInt();
78
79         if (num < 1 || num > CContact::getNum())
80                 return;
81
82         CContact* contact = CContact::getFirst();
83         
84         for (int i = 1; i < num; i++, contact = contact->getNext())
85         {
86                 if (contact->getNext() == '\0')
87                 {
88                         return;
89                 }
90         }
91
92         delete contact;
93         updateView();
94         //qWarning( "MainWindow::slotDel() not yet implemented!" ); 
95 }
96 /* 
97  * public slot
98  */
99 void MainWindow::slotLoad()
100 {
101         filename = QFileDialog::getOpenFileName(0, "Adressfiler (*.adr)", this); 
102
103         QFile f(filename);
104         if ( !f.open( IO_ReadOnly ) )
105                 return;
106                         
107         while (CContact::getNum() != 0)
108         {
109                 delete CContact::getFirst();
110         }
111         
112         QTextStream t(&f);
113
114         while (!t.eof())
115         {
116                 QString fname = t.readLine();
117                 QString lname = t.readLine();
118                 QString adr             = t.readLine();
119                 QString pn              = t.readLine();
120     if (fname == "" && lname == "" && lname == "" && pn == "")
121                               continue;
122                 (void) new CContact(fname, lname, adr, pn);
123         }
124         updateView();
125 }
126 /* 
127  * public slot
128  */
129 void MainWindow::slotSearch()
130 {
131     qWarning( "MainWindow::slotSearch() not yet implemented!" ); 
132 }
133
134 void MainWindow::slotModify(QListViewItem* lvi)
135 {
136         if (lvi == '\0')
137         {
138                 return;
139         }
140         
141         int num = lvi->text(0).toInt();
142
143         if (num < 1 || num > CContact::getNum())
144                 return;
145
146         CContact* contact = CContact::getFirst();
147         
148         for (int i = 1; i < num; i++, contact = contact->getNext())
149         {
150                 if (contact->getNext() == '\0')
151                 {
152                         return;
153                 }
154         }
155         ContactModifier modify(contact);
156         if(modify.exec() == QDialog::Accepted)
157                 updateView();
158 }
159 /*
160  * This funtion puts all contacts in the listview
161  */
162 void MainWindow::updateView()
163 {
164         
165         ContactView->clear();
166
167         CContact* contact = CContact::getFirst();
168
169         int i = 1;
170         char s[100];
171
172         while (contact != '\0')
173         {
174                 sprintf(s,"%i",i);//.arg(i);
175                 (void) new QListViewItem(ContactView, s, contact->lastname(), contact->firstname(), contact->adress(), contact->phoneNumber());
176                 contact = contact->getNext();
177                 i++;
178         }
179 }