Added Database class.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 26 Aug 2010 11:39:47 +0000 (14:39 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 26 Aug 2010 11:39:47 +0000 (14:39 +0300)
src/situareservice/database.cpp [new file with mode: 0644]
src/situareservice/database.h [new file with mode: 0644]
src/situareservice/situareservice.cpp
src/situareservice/situareservice.h
src/src.pro
src/user/user.cpp
src/user/user.h

diff --git a/src/situareservice/database.cpp b/src/situareservice/database.cpp
new file mode 100644 (file)
index 0000000..b9fddc6
--- /dev/null
@@ -0,0 +1,133 @@
+#include <QDebug>
+#include <QDir>
+#include <QSqlQuery>
+#include <QVariant>
+
+#include "database.h"
+
+Database::Database(QObject *parent) :
+    QObject(parent)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+}
+
+Database::~Database()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+}
+
+bool Database::addTag(int userId, const QString &tag)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    bool ret = false;
+
+    if (m_database.isOpen()) {
+        QSqlQuery tagQuery;
+
+        ret = tagQuery.exec(QString("INSERT INTO tag VALUES(NULL, '%1')")
+                         .arg(tag));
+
+        qWarning() << tagQuery.lastError().number();
+        qWarning() << tagQuery.lastError().driverText();
+        qWarning() << tagQuery.lastError().text();
+
+        if (ret) {
+            int tagId = tagQuery.lastInsertId().toInt();
+
+            QSqlQuery userTagQuery;
+
+            ret = userTagQuery.exec(QString("INSERT INTO usertag VALUES(NULL, '%1', '%2')")
+                                    .arg(tagId).arg(userId));
+            qWarning() << userTagQuery.lastError().number();
+            qWarning() << userTagQuery.lastError().driverText();
+            qWarning() << userTagQuery.lastError().text();
+        }
+    }
+
+    qDebug() << ret;
+    return ret;
+}
+
+bool Database::createTagTable()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    bool created = false;
+
+    if (m_database.isOpen()) {
+
+        QSqlQuery query;
+        created = query.exec("CREATE TABLE IF NOT EXISTS tag ("
+                             "id INTEGER PRIMARY KEY,"
+                             "name VARCHAR(30)"
+                             ")");
+
+        qWarning() << query.lastError().text();
+    }
+
+    qDebug() << created;
+    return created;
+}
+
+bool Database::createUserTagTable()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    bool created = false;
+
+    if (m_database.isOpen()) {
+
+        QSqlQuery query;
+        created = query.exec("CREATE TABLE IF NOT EXISTS usertag ("
+                             "id INTEGER PRIMARY KEY,"
+                             "userid INTEGER,"
+                             "tagid INTEGER,"
+                             "FOREIGN KEY(tagid) REFERENCES tag(id)"
+                             ")");
+        qWarning() << query.lastError().text();
+    }
+
+    qDebug() << created;
+    return created;
+}
+
+QStringList Database::getTags(int userId)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QStringList tags;
+
+    QSqlQuery query(QString("SELECT * from usertag WHERE userid = '%1'").arg(userId));
+    qWarning() << query.lastError().text();
+    if (query.next()) {
+        qWarning() << query.value(0).toString();
+        qWarning() << query.value(1).toString();
+        qWarning() << query.value(2).toString();
+    }
+
+    return tags;
+}
+
+bool Database::openDatabase()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_database = QSqlDatabase::addDatabase("QSQLITE");
+
+    QString path(QDir::home().path());
+    path.append(QDir::separator()).append("my.db.sqlite");
+    path = QDir::toNativeSeparators(path);
+    m_database.setDatabaseName(path);
+
+    return m_database.open();
+}
+
+void Database::test()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    bool ret = false;
+
+//    QSqlQuery query(QString("SELECT * from tag"))
+}
diff --git a/src/situareservice/database.h b/src/situareservice/database.h
new file mode 100644 (file)
index 0000000..a1c9137
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef DATABASE_H
+#define DATABASE_H
+
+#include <QObject>
+
+#include <QSqlDatabase>
+#include <QSqlError>
+#include <QFile>
+#include <QStringList>
+
+class Database : public QObject
+{
+    Q_OBJECT
+public:
+    Database(QObject *parent = 0);
+    ~Database();
+
+    bool addTag(int userId, const QString &tag);
+    QStringList getTags(int userId);
+    bool openDatabase();
+    bool createTagTable();
+    bool createUserTagTable();
+    void test();
+
+private:
+    QSqlDatabase m_database;
+};
+
+#endif // DATABASE_H
index 12b56eb..e098332 100644 (file)
@@ -424,6 +424,9 @@ void SituareService::parseUserData(const QByteArray &jsonReply)
             }
             tmpFriendsList.clear();
 
+            //get user tags and set tags to the user
+            m_user->setTags(getTags(m_user->userId()));
+
             emit userDataChanged(m_user, m_friendsList);
 
             // set silhouette image to imageUrlList for downloading
@@ -502,3 +505,15 @@ void SituareService::clearUserData()
     }
     emit userDataChanged(m_user, m_friendsList);
 }
+
+QStringList SituareService::getTags(const QString &userId)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+
+}
+
+void SituareService::updateTags(const QStringList &tags)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+}
index d34b357..a7238f7 100644 (file)
@@ -85,6 +85,14 @@ public:
     */
     void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
 
+    /**
+    * @brief Updates tags to the Situare server
+    *
+    * CURRENTLY TAGS ARE UPDATED TO THE LOCAL DATABASE, NOT SITUARE SERVER
+    * @param tags list of user's tags
+    */
+    void updateTags(const QStringList &tags);
+
 public slots:
 
     /**
@@ -156,6 +164,15 @@ private:
                               bool publish = false);
 
     /**
+    * @brief Temporary method to get tags.
+    *
+    * Tags are fetch from local database instead of Situare server.
+    * @param userId
+    * @return QStringList list of tags
+    */
+    QStringList getTags(const QString &userId);
+
+    /**
     * @brief Parses user and friend data from JSON string
     *
     * @param jsonReply JSON string
index c5de3a6..1b9883d 100644 (file)
@@ -81,7 +81,8 @@ SOURCES += main.cpp \
     ui/routewaypointlistitem.cpp \
     ui/routewaypointlistview.cpp \
     user/user.cpp \
-    ui/meetpeoplepanel.cpp
+    ui/meetpeoplepanel.cpp \
+    situareservice/database.cpp
 HEADERS += application.h \
     common.h \
     coordinates/geocoordinate.h \
@@ -160,9 +161,11 @@ HEADERS += application.h \
     ui/routewaypointlistitem.h \
     ui/routewaypointlistview.h \
     user/user.h \
-    ui/meetpeoplepanel.h
+    ui/meetpeoplepanel.h \
+    situareservice/database.h
 QT += network \
-    webkit
+    webkit \
+    sql
 
 DEFINES += QT_NO_DEBUG_OUTPUT
 
index 03142d0..351a353 100644 (file)
 
 User::User(const QString &address, const GeoCoordinate &coordinates, const QString &name,
            const QString &note, const QUrl &imageUrl, const QString &timestamp, const bool &type,
-           const QString &userId, const QString &units, const double &value)
+           const QString &userId, const QString &units, const double &value,
+           const QStringList &tags = QStringList())
                : m_address(address)
                , m_coordinates(coordinates)
                , m_name(name)
                , m_note(note)
                , m_profileImageUrl(imageUrl)
+               , m_tags(tags)
                , m_timestamp(timestamp)
                , m_type(type)
                , m_units(units)
@@ -44,6 +46,7 @@ User::User()
     , m_name()
     , m_note()
     , m_profileImageUrl()
+    , m_tags()
     , m_timestamp()
     , m_type()
     , m_units()
@@ -84,6 +87,11 @@ void User::setProfileImageUrl(const QUrl &imageUrl)
     m_profileImageUrl = imageUrl;
 }
 
+void User::setTags(const QStringList &tags)
+{
+    m_tags = tags;
+}
+
 void User::setTimestamp(const QString &timestamp)
 {
     m_timestamp = timestamp;
@@ -125,6 +133,11 @@ const QUrl& User::profileImageUrl() const
     return m_profileImageUrl;
 }
 
+const QStringList& User::tags() const
+{
+    return m_tags;
+}
+
 const QString& User::timestamp() const
 {
     return m_timestamp;
index ba37a28..b24c9a1 100644 (file)
@@ -101,6 +101,13 @@ public:
     void setProfileImageUrl(const QUrl &imageUrl);
 
     /**
+    * @brief Sets user's tags.
+    *
+    * @param tags user's tags
+    */
+    void setTags(const QStringList &tags);
+
+    /**
     * @brief Set timestamp for last status update, timestamp is in literal mode
     *
     * @param timestamp timestamp
@@ -158,6 +165,13 @@ public:
     const QUrl &profileImageUrl() const;
 
     /**
+    * @brief Returns user's tags.
+    *
+    * @return QStringList list of tags
+    */
+    const QStringList &tags() const;
+
+    /**
     * @brief Get timestamp of last status update
     *
     * @return QString timestamp
@@ -188,6 +202,7 @@ private:
     QString m_name; ///< placeholder for name
     QString m_note; ///< placeholder for note
     QUrl m_profileImageUrl; ///< placeholder for image url
+    QStringList m_tags;     ///< placeholder for tags
     QString m_timestamp; ///< placeholer for timestamp
     bool m_type; ///< placeholder for user type
     QString m_units; ///< placeholder for distance unit type