Sunday 3 July 2011

keyUserManager.cpp

#include "keyusermanager.h"
#include "QDebug"

KeyUserManager::KeyUserManager(StorageManager *storageManager,KeyPluginsManager *pluginManager,QObject *parent)
    : QObject(parent),
    m_storagemanager(storageManager),
    m_KeyPluginsManager(pluginManager)

{
    m_userInfoTable=m_storageManger->userInfoTable();
}

KeyUserManger::registerDBus()
{
    new KeyUserManagerAdaptor(this);
    QDBusConnection dbus=QDBusConnection::sessionBus();
    dbus.registerObject("/KeyUserManager",this);
    dbus.registerService("org.kde.alkimia.quotes");
  
}

KeyUserManager::~KeyUserManager()
{
      QDBusConnection::sessionBus().unregisterObject("/KeyUserManager");
}

QDBusVariant KeyUserManager::getUserInfo(constant Qstring &user)
{
    QString selectColumns=m_storageManger->userInfoTextColumns().join(",");
    QSqlQuery query(m_storageManager->database());
    query.prepare("SELECT " + selectColumns + " FROM " + m_UserInfoTable + " WHERE user=:user");
    query.bindValue(":user", user);
    query.exec();
    AlkUser userInfo;
    if(query.first() && query.isValid())
    {
        QStringList resultList; // container list for the values to-be-returned

        userInfo.setInfo(query.value(0).toString());
        userInfo.setName(query.value(1).toString());
        userInfo.setType(query.value(2).toString());
        userInfo.setExchange(query.value(3).toString());
  
        qDebug() << "Info: " << userInfo.info();
            qDebug() << "name: " << userInfo.name();
            qDebug() << "type: " << userInfo.type();
            qDebug() << "exchange: " << userInfo.exchange();
    }
     QVariant qVariant = QVariant::fromValue<AlkUser>(userInfo);
      return QDBusVariant(qVariant);
}

void KeyDataManager::insertUserInfo(const QString &user)
{
  QSqlQuery query(m_storageManager->database());
  query.prepare("SELECT user FROM " + m_userInfoTable + " WHERE user=:user");        // i have a little doubt on this statement
  query.bindValue(":user", user);
  query.exec();
 
  if (!query.first() || !query.isValid()) {
    qDebug() << "KeyDataManager::db_insertUser: creating a new entry for: " + user;

    query.prepare("INSERT INTO " + m_userInfoTable + " (account, user) VALUES(NULL, :user)");
    query.bindValue(":user", user);

    // check to make sure the INSERT query worked
    query.exec();
    if (query.lastError().isValid()) {
      qWarning() << "KeyDataManager::db_insertUser failed: " + query.lastError().text();
    }
  }
}

void KeyDataManager::updateUserData(const QString &user, const AlkUser& userInfo, const AlkQuoteItem& quoteItem)
{
  qDebug() << "entering KeyDataManager::db_updateUser: " + user;
  QSqlQuery query(m_storageManager->database());

  insertUserInfo(user);
 
  // Update the user info table
  // even if this user was new, we still need to update its entry in the database with the downloaded values
  QString queryUpdateInfo = "UPDATE " + m_userInfoTable + " SET ";

  // populate queryUpdate with the specific column-value pairs to update for this user
  queryUpdateInfo += "name=:name, type=:type, account=:account, amount=:amount";
 
  queryUpdateInfo += " WHERE user=:user";

  qDebug() << "db_updateUser query: " + queryUpdateInfo;
  query.prepare(queryUpdateInfo);
  
  query.bindValue(":user", user);
  query.bindValue(":name", companyInfo.name());
  query.bindValue(":type", companyInfo.type());
  query.bindValue(":account", companyInfo.account()
  query.bindValue(":amount", companyInfo.amount());
 
  query.exec();
  if (query.lastError().isValid()) {
    qWarning() << "KeyDataManager::db_updateUser failed: " + query.lastError().text();
  }
 
}

No comments:

Post a Comment