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