]> vaikene.ee Git - evaf/commitdiff
Added 'suffix' option, which can be used to generate different password versions...
authorEnar Vaikene <enar.vaikene@logica.com>
Mon, 4 Jun 2012 07:06:36 +0000 (10:06 +0300)
committerEnar Väikene <enar@vaikene.net>
Tue, 5 Jun 2012 06:26:28 +0000 (09:26 +0300)
src/apps/PswGen/Storage/istorage.h
src/apps/PswGen/Storage/module.cpp
src/apps/PswGen/Storage/module.h
src/apps/PswGen/Storage/version.h

index 48226d0ec7a6d66b18c2d1ab7171e991b01c7968..e9abef95850f773a72f147630d5f45aa6cf7768b 100644 (file)
@@ -3,7 +3,7 @@
  * @brief Interface for password storage modules
  * @author Enar Vaikene
  *
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2012 Enar Vaikene
  *
  * This file is part of the eVaf C++ cross-platform application development framework.
  *
@@ -43,14 +43,16 @@ public:
         : QSharedData()
         , mModified(false)
         , mName(name)
+        , mSuffix()
         , mLength(0)
         , mFlags(0)
     {}
 
-    Data(QString const & name, int l, uint f = 0)
+    Data(QString const & name, QString const & suffix, int l, uint f = 0)
         : QSharedData()
         , mModified(false)
         , mName(name)
+        , mSuffix(suffix)
         , mLength(l)
         , mFlags(f)
     {}
@@ -58,6 +60,16 @@ public:
     /// Name of the password
     inline QString const & name() const { return mName; }
 
+    /// Optional suffix added to the name
+    inline QString const & suffix() const { return mSuffix; }
+    void setSuffix(QString const & suffix)
+    {
+        if (suffix != mSuffix) {
+            mSuffix = suffix;
+            mModified = true;
+        }
+    }
+
     /// Length of the generated password
     inline int length() const { return mLength; }
     inline void setLength(int value)
@@ -89,6 +101,7 @@ private:
 
     bool mModified;
     QString mName;
+    QString mSuffix;
     int mLength;
     uint mFlags;
 
index b7676abf282f0d1323855a54919a676048619a43..12fda4213118132b30768af23720beb78cfbd5bd 100644 (file)
@@ -3,7 +3,7 @@
  * @brief Implementation of the iStorage interface
  * @author Enar Vaikene
  *
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2012 Enar Vaikene
  *
  * This file is part of the eVaf C++ cross-platform application development framework.
  *
@@ -150,8 +150,8 @@ bool StorageImpl::save(QString const & name, QExplicitlySharedDataPointer<Storag
         // This is an update
         if (data->modified()) {
             QSqlQuery q(mDb);
-            if (!q.exec(QString("UPDATE data SET length = \'%1\', flags = \'%2\' WHERE name = \'%3\';")
-                            .arg(data->length()).arg(data->flags()).arg(name))) {
+            if (!q.exec(QString("UPDATE data SET suffix = \'%1\', length = \'%2\', flags = \'%3\' WHERE name = \'%4\';")
+                            .arg(data->suffix()).arg(data->length()).arg(data->flags()).arg(name))) {
                 QSqlError err = mDb.lastError();
                 EVAF_ERROR("Failed to update \'%s\' : %s", qPrintable(name), qPrintable(err.text()));
                 return false;
@@ -161,8 +161,8 @@ bool StorageImpl::save(QString const & name, QExplicitlySharedDataPointer<Storag
     else {
         // Store to the database
         QSqlQuery q(mDb);
-        if (!q.exec(QString("INSERT INTO data (name, length, flags) VALUES (\'%1\', %2, %3);")
-                            .arg(name).arg(data->length())
+        if (!q.exec(QString("INSERT INTO data (name, suffix, length, flags) VALUES (\'%1\', \'%2\', %3, %4);")
+                            .arg(name).arg(data->suffix()).arg(data->length())
                             .arg(int(data->flags())))) {
             QSqlError err = mDb.lastError();
             EVAF_ERROR("Failed to insert \'%s\' : %s", qPrintable(name), qPrintable(err.text()));
@@ -210,11 +210,13 @@ bool StorageImpl::createTables()
         return false;
     }
 
-    if (q.isActive() && q.isSelect() && q.first())
-        return true; // We already have a table called 'data'
+    if (q.isActive() && q.isSelect() && q.first()) {
+        // Check if the table needs to be upgraded
+        return upgradeTables();
+    }
 
     // Create the 'data' table
-    if (!q.exec("CREATE TABLE data (name text primary key not null, length integer, flags integer);")) {
+    if (!q.exec("CREATE TABLE data (name text primary key not null, suffix text, length integer, flags integer);")) {
         QSqlError err = mDb.lastError();
         EVAF_ERROR("Failed to create table \'data\' : %s", qPrintable(err.text()));
         return false;
@@ -223,10 +225,29 @@ bool StorageImpl::createTables()
     return true;
 }
 
+bool StorageImpl::upgradeTables()
+{
+    QSqlQuery q(mDb);
+
+    // Check if the 'suffix' column exists
+    if (q.exec("SELECT suffix from data;")) {
+        return true;
+    }
+
+    // Add the 'suffix' columnt
+    if (!q.exec("ALTER TABLE data ADD COLUMN suffix TEXT;")) {
+        QSqlError err = mDb.lastError();
+        EVAF_ERROR("Failed to upgrade table \'data\' : %s", qPrintable(err.text()));
+        return false;
+    }
+
+    return true;
+}
+
 bool StorageImpl::loadData()
 {
     QSqlQuery q(mDb);
-    if (!q.exec("SELECT name, length, flags FROM data;")) {
+    if (!q.exec("SELECT name, suffix, length, flags FROM data;")) {
         QSqlError err = mDb.lastError();
         EVAF_ERROR("Failed to query database : %s", qPrintable(err.text()));
         return false;
@@ -234,7 +255,8 @@ bool StorageImpl::loadData()
 
     while (q.next()) {
         QString name = q.value(0).toString();
-        QExplicitlySharedDataPointer<Storage::Data> data(new Storage::Data(name, q.value(1).toInt(), uint(q.value(2).toInt())));
+        QExplicitlySharedDataPointer<Storage::Data> data(
+                    new Storage::Data(name, q.value(1).toString(), q.value(2).toInt(), uint(q.value(3).toInt())));
         mData.insert(name, data);
     }
 
index 95b935f2926bec0e31793eba401d7d7668c4fbd4..7f53b268d23718b04d65d828a79ce9fbdf998337 100644 (file)
@@ -3,7 +3,7 @@
  * @brief Implementation of the iStorage interface
  * @author Enar Vaikene
  *
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2012 Enar Vaikene
  *
  * This file is part of the eVaf C++ cross-platform application development framework.
  *
@@ -139,6 +139,16 @@ private: // Methods
      */
     bool createTables();
 
+    /**
+     * Upgrades database tables if necessary
+     * @return True if ok; false if failed
+     *
+     * This function checks if database tables need to upgraded and
+     * performs the upgrade without a loss of data if possible.
+     * Returns false if upgrade is not possible or fails.
+     */
+    bool upgradeTables();
+
     /**
      * Loads data from the database
      * @return True if ok; false if failed
index 2712b3680a1efc87efb04f3321cb3c1c25c0980f..cbcf67f9b63218b70da26abf4cc414c251c4761c 100644 (file)
@@ -3,7 +3,7 @@
  * @brief Version information for eVaf modules
  * @author Enar Vaikene
  *
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2012 Enar Vaikene
  *
  * This file is part of the eVaf C++ cross-platform application development framework.
  *
 /**
  * Module/library version number in the form major,minor,release,build
  */
-#define VER_FILE_VERSION                0,1,2,2
+#define VER_FILE_VERSION                0,2,1,3
 
 /**
  * Module/library version number in the string format (shall end with \0)
  */
-#define VER_FILE_VERSION_STR            "0.1.2.2\0"
+#define VER_FILE_VERSION_STR            "0.2.1.3\0"
 
 /**
  * Module/library name (shall end with \0)