-
Notifications
You must be signed in to change notification settings - Fork 4
/
caddrbook.cpp
154 lines (128 loc) · 3.32 KB
/
caddrbook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "caddrbook.h"
#include <QFile>
#include <QTableWidgetItem>
#include <QMessageBox>
#include <QDomDocument>
#include <QTextStream>
#define FILE_NAME "contacts.ini"
CAddrBook::CAddrBook(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
ui.tbl->verticalHeader()->setDefaultSectionSize(20);
connect(ui.cmdAdd, SIGNAL(clicked()), this, SLOT(addAddr()));
connect(ui.cmdDel, SIGNAL(clicked()), this, SLOT(delAddr()));
QFile file(FILE_NAME);
if (!file.open(QFile::ReadOnly | QFile::Text))
return;
QString text;
QIODevice *device = &file;
int errorLine;
int errorColumn;
QDomDocument document;
if (!document.setContent(device, true, &text, &errorLine,
&errorColumn))
{
QMessageBox::information(this, tr("Error reading file"),
QString("Ôàéë: %1, ñòðîêà: %2; %3").arg(text).arg(errorLine).arg(text),
QMessageBox::Ok);
return;
}
file.close();
QTableWidgetItem *newItem;
QDomElement elem = document.documentElement().firstChildElement("contact");
int row = document.documentElement().elementsByTagName("contact").size();
ui.tbl->setRowCount(row);
row = 0;
while (!elem.isNull())
{
newItem = new QTableWidgetItem(elem.attribute("name"));
ui.tbl->setItem(row, 0, newItem);
newItem = new QTableWidgetItem(elem.attribute("addr"));
ui.tbl->setItem(row, 1, newItem);
row++;
elem = elem.nextSiblingElement("contact");
}
}
CAddrBook::~CAddrBook()
{
}
void CAddrBook::closeEvent(QCloseEvent *e)
{
if(!saveIni())
e->ignore();
}
void CAddrBook::accept()
{
if(saveIni())
QDialog::accept();
}
int CAddrBook::saveIni()
{
QDomDocument document;
QDomProcessingInstruction processingInstruction = document.createProcessingInstruction("xml",
"version=\"1.0\" encoding=\"UTF-8\"");
document.appendChild(processingInstruction);
QDomElement rootElement = document.createElement("Contacts");
rootElement.setAttribute("version", "1.0");
document.appendChild(rootElement);
QDomElement elem;
int i, n = ui.tbl->rowCount();
for(i=0;i<n;i++)
{
elem = document.createElement("contact");
elem.setAttribute("name", ui.tbl->item(i, 0)->text());
elem.setAttribute("addr", ui.tbl->item(i, 1)->text());
rootElement.appendChild(elem);
}
QFile file(FILE_NAME);
if (!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::information(this, tr("Error!"),
tr("Can't create file"), QMessageBox::Ok);
return 0;
}
QIODevice * device = &file;
QTextStream out(device);
document.save(out, 4);
file.close();
return 1;
}
int CAddrBook::getAddr(QString &addr)
{
int rc;
rc = exec();
if(rc)
{
QTableWidgetItem *p_item = ui.tbl->currentItem();
if(!p_item)
return 0;
p_item = ui.tbl->item(p_item->row(), 1);
if(!p_item)
return 0;
addr = p_item->text();
}
return rc;
}
void CAddrBook::addAddr()
{
QTableWidgetItem *p_item = ui.tbl->currentItem();
int row = 0;
if(p_item)
row = p_item->row()+1;
ui.tbl->insertRow(row);
}
void CAddrBook::delAddr()
{
int row = ui.tbl->currentRow();
QTableWidgetItem *p_item;
QString name;
p_item = ui.tbl->item(row, 0);
if (p_item)
name = p_item->text();
if (QMessageBox::question(this, tr("Deleting conect"),
tr("Really delete contact \"%1\"?").arg(name),
tr("Yes"), tr("No"), tr("Cancel")) != 0)
return;
ui.tbl->removeRow(row);
}