00001 #include <qpushbutton.h>
00002 #include <qmessagebox.h>
00003 #include <qtextstream.h>
00004 #include <qlistbox.h>
00005 #include <qfile.h>
00006
00007 #include <qpe/resource.h>
00008
00009 #include <stdlib.h>
00010
00011 #include "addresspicker.h"
00012
00013 AddressPicker::AddressPicker(QWidget *parent, const char *name, bool modal,
00014 WFlags fl) : AddressPickerBase(parent, name, modal, fl)
00015 {
00016 okButton->setIconSet(Resource::loadPixmap("enter"));
00017 cancelButton->setIconSet(Resource::loadPixmap("editdelete"));
00018
00019 connect(okButton, SIGNAL(clicked()), SLOT(accept()));
00020 connect(cancelButton, SIGNAL(clicked()), SLOT(close()));
00021
00022 QFile f((QString) getenv("HOME") + "/Applications/"
00023 + "addressbook/addressbook.xml");
00024
00025 if (f.open(IO_ReadOnly)) {
00026 QTextStream stream(&f);
00027 stream.setEncoding( QTextStream::UnicodeUTF8 );
00028 QString content;
00029 while (!f.atEnd()) content += stream.readLine() + "\n";
00030 QStringList lines = QStringList::split(QRegExp("\\n"), content);
00031 QStringList::Iterator it;
00032 for (it = lines.begin(); it != lines.end(); it++) {
00033 if ((*it).find(QRegExp("^<Contact.*")) != -1) {
00034 int pos = (*it).find("FirstName=\"");
00035 QString fname;
00036 if (pos != -1) {
00037 int i = 1;
00038 QChar c;
00039 while (c != '"') {
00040 c = (*it)[pos + 10 + i];
00041 if (c != '"') fname += c;
00042 i++;
00043 }
00044 }
00045 pos = (*it).find("LastName=\"");
00046 QString lname;
00047 if (pos != -1) {
00048 int i = 1;
00049 QChar c;
00050 while (c != '"') {
00051 c = (*it)[pos + 9 + i];
00052 if (c != '"') lname += c;
00053 i++;
00054 }
00055 }
00056 pos = (*it).find("DefaultEmail=\"");
00057 QString email;
00058 if (pos != -1) {
00059 int i = 1;
00060 QChar c;
00061 while (c != '"') {
00062 c = (*it)[pos + 13 + i];
00063 if (c != '"') email += c;
00064 i++;
00065 }
00066 }
00067 QString tname, temail;
00068 if (!fname.isEmpty()) tname += fname;
00069 if (!lname.isEmpty()) tname += fname.isEmpty() ? lname : (" " + lname);
00070 if (!email.isEmpty()) temail += tname.isEmpty() ? email : (" <" + email + ">");
00071 if (!email.isEmpty()) addressList->insertItem(tname + temail);
00072 }
00073 }
00074 }
00075 if (addressList->count() <= 0) {
00076 addressList->insertItem(tr("There are no entries in the addressbook."));
00077 addressList->setEnabled(false);
00078 okButton->setEnabled(false);
00079 }
00080 }
00081
00082 void AddressPicker::accept()
00083 {
00084 QListBoxItem *item = addressList->firstItem();
00085 QString names;
00086
00087 while (item) {
00088 if (item->selected())
00089 names += item->text() + ", ";
00090 item = item->next();
00091 }
00092 names.replace(names.length() - 2, 2, "");
00093
00094 if (names.isEmpty()) {
00095 QMessageBox::information(this, tr("Error"), tr("<p>You have to select"
00096 " at least one address entry.</p>"), tr("Ok"));
00097 return;
00098 }
00099
00100 selectedNames = names;
00101 QDialog::accept();
00102 }
00103
00104 QString AddressPicker::getNames()
00105 {
00106 QString names = 0;
00107
00108 AddressPicker picker(0, 0, true);
00109 picker.showMaximized();
00110 picker.show();
00111
00112 int ret = picker.exec();
00113 if (QDialog::Accepted == ret) {
00114 return picker.selectedNames;
00115 }
00116 return 0;
00117 }
00118