]> ruin.nu Git - adress.git/blob - contact.cpp
a8ffa356c4e85c0c9b4f57b11fd535f26a18a321
[adress.git] / contact.cpp
1 #include "contact.h"
2 #include <cctype>
3 #include <cstring>
4 #include <stl_algo.h>
5
6 CContact* CContact::first = '\0';
7 int CContact::num = 0;
8 /*
9 QString toLower(QString& s)
10 {
11         QString st = s;
12         transform(st.begin(), st.end(), st.begin(), tolower);
13         return st;
14 }*/
15
16
17 CContact::CContact(QString &firstname, QString &lastname, QString &adress, QString &phonenumber)
18 {
19         strings[0] = firstname;
20         strings[1] = lastname;
21         strings[2] = adress;
22         strings[3] = phonenumber;
23
24         num++;
25         next = '\0';
26         prev = '\0';
27         sort();
28         
29 }
30
31 CContact::~CContact()
32 {
33         num--;
34         remove();
35 }
36
37 void CContact::remove()
38 {
39         if (next != '\0' && prev != '\0')
40         {
41                 next->prev = prev;
42                 prev->next = next;
43         }
44         else if (next != '\0' && prev == '\0')
45         {
46                 next->prev = '\0';
47                 first = next;
48         }
49         else if (next == '\0' && prev != '\0')
50         {
51                 prev->next = '\0';
52         }
53         else
54         {
55                 first = '\0';
56         }
57         next = '\0';
58         prev = '\0';
59 }
60         
61 void CContact::sort()
62 {
63         if (first == '\0')
64         {
65                 first = this;
66                 prev = '\0';
67                 next = '\0';
68                 return;
69         }
70         CContact* contact = first;
71         while (contact != '\0')
72         {
73                 int test = strings[1].upper().compare(contact->strings[1].upper());
74                 
75                 if (test == 0)
76                 {
77                         //int test2 = strcasecmp(strfdsfdngs[0].c_str(), contact->strings[0].c_str());
78                         int test2 = strings[0].upper().compare(contact->strings[0].upper());
79                         
80                         if (test2 < 0)
81                         {
82                                 next = contact;
83                                 if (first == contact)
84                                 {
85                                         first = this;
86                                         prev = '\0';
87                                         contact->prev = this;
88                                 }
89                                 else
90                                 {
91                                         prev = contact->prev;
92                                         prev->next = this;
93                                         contact->prev = this;
94                                 }
95                                 break;
96                         }
97                 }
98         
99                 if (test < 0)
100                 {
101                         next = contact;
102                         if (first == contact)
103                         {
104                                 first = this;
105                                 prev = '\0';
106                                 contact->prev = this;
107                         }
108                         else
109                         {
110                                 prev = contact->prev;
111                                 prev->next = this;
112                                 contact->prev = this;
113                         }
114                         break;
115                 }
116                 if (contact->next == '\0')
117                 {
118                         prev = contact;
119                         next = '\0';
120                         contact->next = this;
121
122                         break;
123                 }
124         
125                 contact = contact->next;
126         }
127 }
128         
129 //Some functions
130 QString CContact::firstname()
131 {
132         return strings[0];
133 }
134
135 QString CContact::lastname()
136 {
137         return strings[1];
138 }
139
140 QString CContact::adress()
141 {
142         return strings[2];
143 }
144
145 QString CContact::phoneNumber()
146 {
147         return strings[3];
148 }
149
150 bool CContact::search(int i, QString s)
151 {
152         if (i > 0 && i < 5)
153         {
154                 //char* s1 = s.c_str();
155                 int test = strings[i - 1 ].find(s,0,false );
156 //              cout << strings[i - 1] << "\n" << s << "\n" << test << "\nTryck Enter";
157 //              cin.get();
158                 if (test == 0) //QString::npos) //4294967295)
159                         return true;
160                 else
161                         return false;
162         }
163         else
164                 return false;
165 }
166
167 void CContact::modify(int i, QString s)
168 {
169         if (i > 0 && i < 5)
170         {
171 //              cout << "Modifierar strängen " << i << " med namnet " << strings[0] << " \n";
172                 strings[i - 1] = s;
173                 if (i == 2)
174                 {
175                 //      cout << "Tar bort gammal konfiguration\n";
176                         remove();
177                 //      cout << "Lägger till den på rätt plats+n";
178                         sort();
179                 }
180         }
181 }
182 //Static functions
183 CContact* CContact::getFirst()
184 {
185         return first;
186 }
187
188 int CContact::getNum()
189 {
190         return num;
191 }
192
193
194 CContact* CContact::getNext()
195 {
196         return next;
197 }
198
199 CContact* CContact::getPrev()
200 {
201         return prev;
202 }