]> vaikene.ee Git - evaf/blobdiff - src/apps/PswGen/CLI/cli.cpp
Implementing 'suffix' and alpha-numeric options in the PswGen GUI and CLI.
[evaf] / src / apps / PswGen / CLI / cli.cpp
index cd18b852b7f096660a5ac1ecf6130b12242d4f11..51ea2b6690351cafa0dc2f654156a856fd2a03b8 100644 (file)
@@ -3,7 +3,7 @@
  * @brief Command line interface for the PswGen application
  * @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.
  *
@@ -29,6 +29,7 @@
 #include <Common/iEventQueue>
 #include <Common/iApp>
 #include <Common/Event>
+#include <Common/Util>
 
 #include <QtCore>
 
@@ -159,7 +160,10 @@ void Module::generatePassword()
 {
     QString masterPassword;
     QString appName;
+    QString suffix;
     int passwordLength = 0;
+    uint flags = 0;
+    int alnum = -1;
 
     // Process command-line arguments
     QStringList args = QCoreApplication::arguments();
@@ -170,6 +174,18 @@ void Module::generatePassword()
             masterPassword = arg.at(1);
         else if (QRegExp("-[-]?n(ame)?").exactMatch(arg.at(0)) && arg.size() > 1)
             appName = arg.at(1);
+        else if (QRegExp("-[-]?s(uffix)?").exactMatch(arg.at(0)) && arg.size() > 1)
+            suffix = arg.at(1);
+        else if (QRegExp("-[-]?a(lphanumeric)?").exactMatch(arg.at(0))) {
+            if (arg.size() > 1) {
+                if (Common::isTrue(arg.at(1)))
+                    alnum = 1;
+                else if (Common::isFalse(arg.at(1)))
+                    alnum = 0;
+            }
+            else
+                alnum = 1;
+        }
         else if (QRegExp("-[-]?l(ength)?").exactMatch(arg.at(0)) && arg.size() > 1) {
             bool ok;
             int t = arg.at(1).toInt(&ok);
@@ -181,6 +197,14 @@ void Module::generatePassword()
         }
     }
 
+    // Set flags
+    if (alnum != -1) {
+        if (alnum == 1)
+            flags |= uint(iGenerator::ALPHANUMERIC);
+        else
+            flags &= ~uint(iGenerator::ALPHANUMERIC);
+    }
+
     QTextStream cin(stdin);
     QTextStream cout(stdout);
 
@@ -199,8 +223,14 @@ void Module::generatePassword()
     QExplicitlySharedDataPointer<PswGen::Storage::Data> data;
     if (mStorage) {
         data = mStorage->query(appName);
-        if (data && passwordLength == 0)
-            passwordLength = data->length();
+        if (data) {
+            if (passwordLength == 0)
+                passwordLength = data->length();
+            if (suffix.isEmpty())
+                suffix = data->suffix();
+            if (alnum == -1)
+                flags = data->flags();
+        }
     }
 
     // If the length argument is still not initialized, use the default length value
@@ -208,15 +238,17 @@ void Module::generatePassword()
         passwordLength = DefaultPasswordLength;
 
     // Generate password
-    QString password = mGenerator->generatePassword(appName, masterPassword, passwordLength);
+    QString password = mGenerator->generatePassword(appName + suffix, masterPassword, passwordLength);
     cout << "Generated password : " << password << endl;
 
     // Store arguments for this password
     if (mStorage) {
         if (!data)
-            data = new Storage::Data(appName, passwordLength);
-        else
+            data = new Storage::Data(appName, suffix, passwordLength);
+        else {
+            data->setSuffix(suffix);
             data->setLength(passwordLength);
+        }
         mStorage->save(appName, data);
     }
 }