source: SMSSender/src/persistence/storage/DAAccountListManager.cpp @ 43:10dd65d71f7f

3.0
Last change on this file since 43:10dd65d71f7f was 43:10dd65d71f7f, checked in by Sämy Zehnder <saemy.zehnder@…>, 12 years ago
  • Did a lot of work...
File size: 1.9 KB
Line 
1/*
2 * DAAccountListManager.cpp
3 *
4 *  Created on: Aug 30, 2009
5 *      Author: saemy
6 */
7
8#include "DAAccountListManager.h"
9
10#include <QSqlQuery>
11
12#include <igateway.h>
13#include <StorageExceptions.h>
14#include <typeconvert.h>
15
16#include "../../business/BCGatewayManager.h"
17
18#include "DAStorage.h"
19
20DAAccountListManager* DAAccountListManager::instance_=0;
21DAAccountListManager* DAAccountListManager::instance(){
22    return instance_ ? instance_ : (instance_ = new DAAccountListManager);
23}
24
25
26IAccount* DAAccountListManager::getAccount(int accountId, bool skeletonOnly /* = false */) {
27    QSqlQuery query = storage()->createQuery();
28    query.exec("SELECT type "
29                 "FROM t_accounts "
30                 "WHERE (accountId = '" + QString::number(accountId) + "'); ");
31    if (!query.next()) {
32        throw Storage::EReadException(tr("No such account in the database! [accountId: %1]").arg(accountId));
33    }
34
35    IGateway* gateway = BCGatewayManager::instance()->getGateway(query.value(0).toString());
36
37    // TODO: What if this gateway is NULL (is not loaded)?
38    gateway->setDefaultStorage(storage());
39
40    IAccount* account = gateway->createAccountInstance();
41    account->setId(accountId);
42
43    if (!skeletonOnly) {
44        account->getStorageOfficer()->readFromStorage();
45    }
46
47    return account;
48}
49
50QSet<IAccount*> DAAccountListManager::getAccountList(bool skeletonsOnly /* = false */) {
51    QSet<IAccount*> accountList;
52
53    QSqlQuery query = storage()->createQuery();
54    query.exec("SELECT accountId "
55                 "FROM t_accounts;");
56    while (query.next()) {
57        try{
58            IAccount* account = getAccount(query.value(0).toInt(), skeletonsOnly);
59            accountList.insert(account);
60        }catch (const Storage::EReadException& e){
61            // TODO: What shall be done here?
62        }
63    }
64
65    return accountList;
66}
67
68
69IStorage* DAAccountListManager::storage() {
70    return DAStorage::instance();
71}
Note: See TracBrowser for help on using the repository browser.