changeset 444:66ec8b2ee946

Provide auth key and file information to easyhg extension
author Chris Cannam
date Tue, 28 Jun 2011 15:59:13 +0100
parents 459b140032d4
children ff6252986354
files easyhg.py src/common.h src/hgrunner.cpp src/hgrunner.h src/moreinformationdialog.cpp
diffstat 5 files changed, 68 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/easyhg.py	Tue Jun 28 14:33:50 2011 +0100
+++ b/easyhg.py	Tue Jun 28 15:59:13 2011 +0100
@@ -90,19 +90,19 @@
     pcfg.readfp(fp)
     fp.close()
 
-def save_config(pcfg, pfile):
+def save_config(ui, pcfg, pfile):
     ofp = None
     try:
         ofp = open(pfile, 'w')
     except:
-        self.ui.write("failed to open authfile %s for writing\n" % pfile)
+        ui.write("failed to open authfile %s for writing\n" % pfile)
         raise
     try:
         #!!! Windows equivalent?
         os.fchmod(ofp.fileno(), stat.S_IRUSR | stat.S_IWUSR)
     except:
         ofp.close()
-        self.ui.write("failed to set permissions on authfile %s\n" % pfile)
+        ui.write("failed to set permissions on authfile %s\n" % pfile)
         raise
     pcfg.write(ofp)
     ofp.close()
@@ -212,7 +212,7 @@
             passwd_field.setText(cachedpwd)
         remember_field = QtGui.QCheckBox()
         remember_field.setChecked(remember)
-        remember_field.setText(_('Remember this password until EasyMercurial exits'))
+        remember_field.setText(_('Remember passwords while EasyMercurial is running'))
         layout.addWidget(remember_field, 3, 1)
 
     bb = QtGui.QDialogButtonBox()
@@ -250,7 +250,7 @@
                 set_to_config(authconfig, 'auth', remote_key(short_uri, user), authdata)
             else:
                 set_to_config(authconfig, 'auth', remote_key(short_uri, user), '')
-        save_config(authconfig, authfile)
+        save_config(self.ui, authconfig, authfile)
 
     self.add_password(realm, authuri, user, passwd)
     return (user, passwd)
--- a/src/common.h	Tue Jun 28 14:33:50 2011 +0100
+++ b/src/common.h	Tue Jun 28 15:59:13 2011 +0100
@@ -22,9 +22,6 @@
 
 extern QString findInPath(QString name, QString installPath, bool executable);
 
-extern QString getSystem();
-extern QString getHgDirName();
-
 extern QString getUserRealName();
 
 extern void loseControllingTerminal();
--- a/src/hgrunner.cpp	Tue Jun 28 14:33:50 2011 +0100
+++ b/src/hgrunner.cpp	Tue Jun 28 15:59:13 2011 +0100
@@ -27,6 +27,7 @@
 #include <QVBoxLayout>
 #include <QSettings>
 #include <QInputDialog>
+#include <QDesktopServices>
 #include <QTemporaryFile>
 #include <QDir>
 
@@ -68,6 +69,10 @@
         m_proc->kill();
         m_proc->deleteLater();
     }
+    if (m_authFilePath != "") {
+        QFile(m_authFilePath).remove();
+    }
+    //!!! and remove any other misc auth file paths...
 }
 
 QString HgRunner::getUnbundledFileName()
@@ -386,6 +391,57 @@
     startCommand(toRun);
 }
 
+QStringList HgRunner::addExtensionOptions(QStringList params)
+{
+    QString extpath = getExtensionLocation();
+    if (extpath == "") {
+        DEBUG << "HgRunner::addExtensionOptions: Failed to get extension location" << endl;
+        return params;
+    }
+    
+    if (m_authFilePath == "") {
+
+        QByteArray key = randomKey();
+        if (key == QByteArray()) {
+            DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth key" << endl;
+            return params;
+        }
+        QString key16 = QString::fromLocal8Bit(key.toBase64()).left(16);
+
+        QByteArray fileExt = randomKey();
+        if (fileExt == QByteArray()) {
+            DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth file ext" << endl;
+            return params;
+        }
+        QString fileExt16 = QString::fromLocal8Bit(fileExt.toBase64()).left(16)
+            .replace('+', '-').replace('/', '.');
+        QString path = QDesktopServices::storageLocation
+            (QDesktopServices::CacheLocation);
+        QDir().mkpath(path);
+        if (path == "") {
+            DEBUG << "HgRunner::addExtensionOptions: Failed to get cache location" << endl;
+            return params;
+        }
+
+        m_authKey = key16;
+        m_authFilePath = QString("%1/easyhg_%2.dat").arg(path).arg(fileExt16);
+    }
+
+    params.push_front(QString("easyhg.authkey=%1").arg(m_authKey));
+    params.push_front("--config");
+
+    params.push_front(QString("easyhg.authfile=%1").arg(m_authFilePath));
+    params.push_front("--config");
+
+    // Looks like this one must be without quotes, even though the SSH
+    // one above only works on Windows if it has quotes (at least where
+    // there is a space in the path).  Odd
+    params.push_front(QString("extensions.easyhg=%1").arg(extpath));
+    params.push_front("--config");
+
+    return params;
+}
+
 void HgRunner::startCommand(HgAction action)
 {
     QString executable = action.executable;
@@ -415,14 +471,8 @@
         if (action.mayBeInteractive()) {
             params.push_front("ui.interactive=true");
             params.push_front("--config");
-
             if (settings.value("useextension", true).toBool()) {
-                QString extpath = getExtensionLocation();
-                // Looks like this one must be without quotes, even though the SSH
-                // one above only works on Windows if it has quotes (at least where
-                // there is a space in the path).  Odd
-                params.push_front(QString("extensions.easyhg=%1").arg(extpath));
-                params.push_front("--config");
+                params = addExtensionOptions(params);
             }
             interactive = true;
         }            
--- a/src/hgrunner.h	Tue Jun 28 14:33:50 2011 +0100
+++ b/src/hgrunner.h	Tue Jun 28 15:59:13 2011 +0100
@@ -74,6 +74,8 @@
     QString getUnbundledFileName();
     QString unbundleExtension();
 
+    QStringList addExtensionOptions(QStringList);
+
     int m_ptyMasterFd;
     int m_ptySlaveFd;
     QString m_ptySlaveFilename;
@@ -90,6 +92,9 @@
     QString m_myDirPath;
     QString m_extensionPath;
 
+    QString m_authKey;
+    QString m_authFilePath;
+
     typedef std::deque<HgAction> ActionQueue;
     ActionQueue m_queue;
     HgAction m_currentAction;
--- a/src/moreinformationdialog.cpp	Tue Jun 28 14:33:50 2011 +0100
+++ b/src/moreinformationdialog.cpp	Tue Jun 28 15:59:13 2011 +0100
@@ -79,7 +79,7 @@
     m_moreText->hide();
     if (more == "") m_moreButton->hide();
 
-    layout->setRowStretch(1, 20);
+    layout->setRowStretch(1, 10);
     layout->setColumnStretch(1, 20);
     setMinimumWidth(400);
 }