qtmobility/examples/samplephonebook/contactlistpage.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    47 #include "qversitcontactexporter.h"
    47 #include "qversitcontactexporter.h"
    48 #endif
    48 #endif
    49 
    49 
    50 #include <QtGui>
    50 #include <QtGui>
    51 
    51 
    52 ContactListPage::ContactListPage(QWidget *parent)
    52 ContactListPage::ContactListPage(QMainWindow *mainWindow, QWidget *parent)
    53         : QWidget(parent)
    53         : QWidget(parent), m_mainWindow(mainWindow)
    54 {
    54 {
    55     m_manager = 0;
    55     m_manager = 0;
    56     m_currentFilter = QContactFilter();
    56     m_currentFilter = QContactFilter();
    57 
    57 
    58     m_backendsCombo = new QComboBox(this);
    58     m_backendsCombo = new QComboBox(this);
    59     QStringList availableManagers = QContactManager::availableManagers();
    59     QStringList availableManagers = QContactManager::availableManagers();
       
    60     availableManagers.removeAll("invalid");
       
    61     foreach(QString managerName, availableManagers) {
       
    62 
       
    63         QMap<QString, QString> params;
       
    64         QString managerUri = QContactManager::buildUri(managerName, params);
       
    65 
       
    66         // Add some parameters to SIM backend so that we can use
       
    67         // all the stores.
       
    68         if (managerName == "symbiansim") {
       
    69             availableManagers.removeAll("symbiansim");
       
    70 
       
    71             availableManagers.append("symbiansim:adn");
       
    72             params.insert("store", "ADN");
       
    73             managerUri = QContactManager::buildUri(managerName, params);
       
    74             m_availableManagers.insert(availableManagers.last(), managerUri);
       
    75 
       
    76             availableManagers.append("symbiansim:fdn");
       
    77             params.clear();
       
    78             params.insert("store", "FDN");
       
    79             managerUri = QContactManager::buildUri(managerName, params);
       
    80             m_availableManagers.insert(availableManagers.last(), managerUri);
       
    81 
       
    82             availableManagers.append("symbiansim:sdn");
       
    83             params.clear();
       
    84             params.insert("store", "SDN");
       
    85             managerUri = QContactManager::buildUri(managerName, params);
       
    86             m_availableManagers.insert(availableManagers.last(), managerUri);
       
    87         }
       
    88         else {
       
    89             m_availableManagers.insert(managerName, managerUri);
       
    90         }
       
    91     }
    60     m_backendsCombo->addItems(availableManagers);
    92     m_backendsCombo->addItems(availableManagers);
    61     connect(m_backendsCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(backendSelected()));
    93     connect(m_backendsCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(backendSelected()));
    62     m_filterActiveLabel = new QLabel("Inactive");
    94     m_filterActiveLabel = new QLabel(tr("Filter active"));
    63     m_filterActiveLabel->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    95     m_filterActiveLabel->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    64 
    96 
       
    97     QVBoxLayout *bookLayout = new QVBoxLayout;
       
    98     QFormLayout *backendLayout = new QFormLayout;
       
    99     backendLayout->addRow(tr("Store:"), m_backendsCombo);
       
   100     backendLayout->addRow(m_filterActiveLabel);
       
   101     bookLayout->addLayout(backendLayout);
       
   102 
    65     m_contactsList = new QListWidget(this);
   103     m_contactsList = new QListWidget(this);
    66 
   104     bookLayout->addWidget(m_contactsList);
    67     QPushButton* m_addContactBtn = new QPushButton("Add", this);
   105 
    68     connect(m_addContactBtn, SIGNAL(clicked()), this, SLOT(addContactClicked()));
   106     // Action buttons at the bottom
    69     QPushButton* m_editBtn = new QPushButton("Edit", this);
       
    70     connect(m_editBtn, SIGNAL(clicked()), this, SLOT(editClicked()));
       
    71     QPushButton* m_deleteBtn = new QPushButton("Delete", this);
       
    72     connect(m_deleteBtn, SIGNAL(clicked()), this, SLOT(deleteClicked()));
       
    73     QPushButton* m_filterBtn = new QPushButton("Filter", this);
       
    74     connect(m_filterBtn, SIGNAL(clicked()), this, SLOT(filterClicked()));
       
    75     QPushButton* m_importBtn = new QPushButton("Import");
       
    76     connect(m_importBtn, SIGNAL(clicked()), this, SLOT(importClicked()));
       
    77     QPushButton* m_exportBtn = new QPushButton("Export");
       
    78     connect(m_exportBtn, SIGNAL(clicked()), this, SLOT(exportClicked()));
       
    79 
       
    80 
       
    81     QFormLayout *backendLayout = new QFormLayout;
       
    82     backendLayout->addRow("Store:", m_backendsCombo);
       
    83     backendLayout->addRow("Filter:", m_filterActiveLabel);
       
    84 
       
    85     QHBoxLayout *btnLayout1 = new QHBoxLayout;
   107     QHBoxLayout *btnLayout1 = new QHBoxLayout;
    86     btnLayout1->addWidget(m_addContactBtn);
   108 
    87     btnLayout1->addWidget(m_editBtn);
   109     QPushButton* addBtn = new QPushButton(tr("&Add"), this);
    88     btnLayout1->addWidget(m_deleteBtn);
   110     connect(addBtn, SIGNAL(clicked()), this, SLOT(addClicked()));
    89     btnLayout1->addWidget(m_filterBtn);
   111     btnLayout1->addWidget(addBtn);
    90 
   112 
    91     QHBoxLayout *btnLayout2 = new QHBoxLayout;
   113     QPushButton* editBtn = new QPushButton(tr("&Edit"), this);
    92     btnLayout2->addWidget(m_importBtn);
   114     connect(editBtn, SIGNAL(clicked()), this, SLOT(editClicked()));
    93     btnLayout2->addWidget(m_exportBtn);
   115     btnLayout1->addWidget(editBtn);
    94 
   116 
    95     QVBoxLayout *bookLayout = new QVBoxLayout;
   117     QPushButton* deleteBtn = new QPushButton(tr("&Delete"), this);
    96     bookLayout->addLayout(backendLayout);
   118     connect(deleteBtn, SIGNAL(clicked()), this, SLOT(deleteClicked()));
    97     bookLayout->addWidget(m_contactsList);
   119     btnLayout1->addWidget(deleteBtn);
       
   120 
    98     bookLayout->addLayout(btnLayout1);
   121     bookLayout->addLayout(btnLayout1);
       
   122 
       
   123     setLayout(bookLayout);
       
   124 
       
   125     // Add items to the menu
       
   126     if (m_mainWindow) {
       
   127 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
   128         QMenuBar *optionsMenu = m_mainWindow->menuBar();
       
   129 #else
       
   130         QMenu *optionsMenu = new QMenu(tr("&Contacts"), this);
       
   131         m_mainWindow->menuBar()->addMenu(optionsMenu);
       
   132 #endif
       
   133         QAction* addAction = new QAction(tr("&Add Contact..."), this);
       
   134         connect(addAction, SIGNAL(triggered()), this, SLOT(addClicked()));
       
   135         optionsMenu->addAction(addAction);
       
   136         QAction* editAction = new QAction(tr("&Edit Contact..."), this);
       
   137         connect(editAction, SIGNAL(triggered()), this, SLOT(editClicked()));
       
   138         optionsMenu->addAction(editAction);
       
   139         QAction* deleteAction = new QAction(tr("&Delete Contact"), this);
       
   140         connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteClicked()));
       
   141         optionsMenu->addAction(deleteAction);
       
   142         optionsMenu->addSeparator();
       
   143         QAction* filterAction = new QAction(tr("Apply &Filter..."), this);
       
   144         connect(filterAction, SIGNAL(triggered()), this, SLOT(filterClicked()));
       
   145         optionsMenu->addAction(filterAction);
       
   146         QAction* clearFilterAction = new QAction(tr("&Clear Filter"), this);
       
   147         connect(clearFilterAction, SIGNAL(triggered()), this, SIGNAL(clearFilter()));
       
   148         optionsMenu->addAction(clearFilterAction);
       
   149         optionsMenu->addSeparator();
       
   150 
    99 #ifdef BUILD_VERSIT
   151 #ifdef BUILD_VERSIT
   100     bookLayout->addLayout(btnLayout2);
   152         QAction* importAction = new QAction(tr("&Import contacts..."), this);
   101 #endif
   153         connect(importAction, SIGNAL(triggered()), this, SLOT(importClicked()));
   102 
   154         optionsMenu->addAction(importAction);
   103     setLayout(bookLayout);
   155         QAction* exportAction = new QAction(tr("Ex&port contacts..."), this);
       
   156         connect(exportAction, SIGNAL(triggered()), this, SLOT(exportClicked()));
       
   157         optionsMenu->addAction(exportAction);
       
   158         optionsMenu->addSeparator();
       
   159 #endif
       
   160         QAction* exitAction = new QAction(tr("E&xit"), this);
       
   161         connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
       
   162         optionsMenu->addAction(exitAction);
       
   163     }
   104 
   164 
   105     // force update to backend.
   165     // force update to backend.
   106     QTimer::singleShot(0, this, SLOT(backendSelected()));
   166     QTimer::singleShot(0, this, SLOT(backendSelected()));
   107 }
   167 }
   108 
   168 
   115     }
   175     }
   116 }
   176 }
   117 
   177 
   118 void ContactListPage::backendSelected()
   178 void ContactListPage::backendSelected()
   119 {
   179 {
   120     QString backend = m_backendsCombo->currentText();
   180     QString managerUri = m_availableManagers.value(m_backendsCombo->currentText());
   121 
   181 
   122     // first, check to see if they reselected the same backend.
   182     // first, check to see if they reselected the same backend.
   123     if (m_manager && m_manager->managerName() == backend)
   183     if (m_manager && m_manager->managerUri() == managerUri)
   124         return;
   184         return;
   125 
   185 
   126     // the change is real.  update.
   186     // the change is real.  update.
   127     if (m_initialisedManagers.contains(backend)) {
   187     if (m_initialisedManagers.contains(managerUri)) {
   128         m_manager = m_initialisedManagers.value(backend);
   188         m_manager = m_initialisedManagers.value(managerUri);
   129     } else {
   189     } else {
   130         m_manager = new QContactManager(backend);
   190         m_manager = QContactManager::fromUri(managerUri);
   131         m_initialisedManagers.insert(backend, m_manager);
   191         if (m_manager->error()) {
       
   192             QMessageBox::information(this, tr("Failed!"), QString("Failed to open store!\n(error code %1)").arg(m_manager->error()));
       
   193             delete m_manager;
       
   194             m_manager = 0;
       
   195             return;
       
   196         }
       
   197         m_initialisedManagers.insert(managerUri, m_manager);
   132     }
   198     }
   133 
   199 
   134     // signal that the manager has changed.
   200     // signal that the manager has changed.
   135     emit managerChanged(m_manager);
   201     emit managerChanged(m_manager);
   136 
   202 
   138     rebuildList(m_currentFilter);
   204     rebuildList(m_currentFilter);
   139 }
   205 }
   140 
   206 
   141 void ContactListPage::rebuildList(const QContactFilter& filter)
   207 void ContactListPage::rebuildList(const QContactFilter& filter)
   142 {
   208 {
   143     // first, check to see whether the filter does anything
   209     m_currentFilter = QContactManagerEngine::canonicalizedFilter(filter);
   144     if (filter == QContactFilter())
   210 
   145         m_filterActiveLabel->setText("Inactive");
   211     m_filterActiveLabel->setVisible(m_currentFilter != QContactFilter());
   146     else
       
   147         m_filterActiveLabel->setText("Active");
       
   148 
   212 
   149     QContact currContact;
   213     QContact currContact;
   150     m_currentFilter = filter;
       
   151     m_contactsList->clear();
   214     m_contactsList->clear();
   152     m_idToListIndex.clear();
   215     m_idToListIndex.clear();
   153     QList<QContactLocalId> contactIds = m_manager->contactIds(m_currentFilter);
   216     QList<QContactLocalId> contactIds = m_manager->contactIds(m_currentFilter);
   154     foreach (const QContactLocalId& id, contactIds) {
   217     foreach (const QContactLocalId& id, contactIds) {
   155         QListWidgetItem *currItem = new QListWidgetItem;
   218         QListWidgetItem *currItem = new QListWidgetItem;
   159         m_idToListIndex.insert(currContact.localId(), m_contactsList->count());
   222         m_idToListIndex.insert(currContact.localId(), m_contactsList->count());
   160         m_contactsList->addItem(currItem);
   223         m_contactsList->addItem(currItem);
   161     }
   224     }
   162 }
   225 }
   163 
   226 
   164 void ContactListPage::addContactClicked()
   227 void ContactListPage::addClicked()
   165 {
   228 {
   166     emit showEditorPage(QContactLocalId(0));
   229     if (m_manager)
       
   230         emit showEditorPage(QContactLocalId(0));
   167 }
   231 }
   168 
   232 
   169 void ContactListPage::editClicked()
   233 void ContactListPage::editClicked()
   170 {
   234 {
   171     if (m_contactsList->currentItem())
   235     if (m_contactsList->currentItem())
   173     // else, nothing selected; ignore.
   237     // else, nothing selected; ignore.
   174 }
   238 }
   175 
   239 
   176 void ContactListPage::filterClicked()
   240 void ContactListPage::filterClicked()
   177 {
   241 {
   178     emit showFilterPage(m_currentFilter);
   242     if (m_manager)
       
   243         emit showFilterPage(m_currentFilter);
   179 }
   244 }
   180 
   245 
   181 void ContactListPage::deleteClicked()
   246 void ContactListPage::deleteClicked()
   182 {
   247 {
   183     if (!m_manager) {
   248     if (!m_manager) {
   187 
   252 
   188     if (!m_contactsList->currentItem()) {
   253     if (!m_contactsList->currentItem()) {
   189         qWarning() << "Nothing to delete.";
   254         qWarning() << "Nothing to delete.";
   190         return;
   255         return;
   191     }
   256     }
   192         
   257 
   193     QContactLocalId contactId = QContactLocalId(m_contactsList->currentItem()->data(Qt::UserRole).toUInt());
   258     QContactLocalId contactId = QContactLocalId(m_contactsList->currentItem()->data(Qt::UserRole).toUInt());
   194     bool success = m_manager->removeContact(contactId);
   259     bool success = m_manager->removeContact(contactId);
   195     if (success) {
   260     if (success) {
   196         delete m_contactsList->takeItem(m_contactsList->currentRow());
   261         delete m_contactsList->takeItem(m_contactsList->currentRow());
   197         QMessageBox::information(this, "Success!", "Contact deleted successfully!");
       
   198     }
   262     }
   199     else
   263     else
   200         QMessageBox::information(this, "Failed!", "Failed to delete contact!");
   264         QMessageBox::information(this, "Failed!", "Failed to delete contact!");
   201 }
   265 }
   202 
   266 
   214     if (file.isReadable()) {
   278     if (file.isReadable()) {
   215         QVersitReader reader;
   279         QVersitReader reader;
   216         reader.setDevice(&file);
   280         reader.setDevice(&file);
   217         if (reader.startReading() && reader.waitForFinished()) {
   281         if (reader.startReading() && reader.waitForFinished()) {
   218             QVersitContactImporter importer;
   282             QVersitContactImporter importer;
   219             QList<QContact> contacts = importer.importContacts(reader.results());
   283             if (importer.importDocuments(reader.results())) {
   220             QMap<int, QContactManager::Error> errorMap;
   284                 QList<QContact> contacts = importer.contacts();
   221             m_manager->saveContacts(&contacts, &errorMap);
   285                 QMap<int, QContactManager::Error> errorMap;
   222             rebuildList(m_currentFilter);
   286                 m_manager->saveContacts(&contacts, &errorMap);
       
   287                 rebuildList(m_currentFilter);
       
   288             }
   223         }
   289         }
   224     }
   290     }
   225 #endif
   291 #endif
   226 }
   292 }
   227 
   293 
   230 #ifdef BUILD_VERSIT
   296 #ifdef BUILD_VERSIT
   231     if (!m_manager) {
   297     if (!m_manager) {
   232         qWarning() << "No manager selected; cannot import";
   298         qWarning() << "No manager selected; cannot import";
   233         return;
   299         return;
   234     }
   300     }
   235     QList<QContact> contacts = m_manager->contacts(QList<QContactSortOrder>(), QStringList());
   301     QList<QContact> contacts = m_manager->contacts(QList<QContactSortOrder>(), QContactFetchHint());
   236     QString fileName = QFileDialog::getSaveFileName(this, tr("Save vCard"),
   302     QString fileName = QFileDialog::getSaveFileName(this, tr("Save vCard"),
   237                                                     "./contacts.vcf",
   303                                                     "./contacts.vcf",
   238                                                     tr("vCards (*.vcf)"));
   304                                                     tr("vCards (*.vcf)"));
   239     QFile file(fileName);
   305     QFile file(fileName);
   240     file.open(QIODevice::WriteOnly);
   306     file.open(QIODevice::WriteOnly);
   241     if (file.isWritable()) {
   307     if (file.isWritable()) {
   242         QVersitContactExporter exporter;
   308         QVersitContactExporter exporter;
   243         QList<QVersitDocument> documents = exporter.exportContacts(contacts);
   309         if(exporter.exportContacts(contacts, QVersitDocument::VCard30Type)) {
   244         QVersitWriter writer;
   310             QList<QVersitDocument> documents = exporter.documents();
   245         writer.setDevice(&file);
   311             QVersitWriter writer;
   246         writer.startWriting(documents);
   312             writer.setDevice(&file);
   247         writer.waitForFinished();
   313             writer.startWriting(documents);
   248     }
   314             writer.waitForFinished();
   249 #endif
   315         }
   250 }
   316     }
       
   317 #endif
       
   318 }