summaryrefslogtreecommitdiffstats
path: root/src/helpers
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-03-15 17:32:48 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-03-15 17:32:48 +0000
commite2f541c98dfa4081fa3ab3d28f08ea2309281884 (patch)
treecb721a55bc88753ddeb9754dc98ef45e2850ce30 /src/helpers
downloadtdesvn-e2f541c98dfa4081fa3ab3d28f08ea2309281884.tar.gz
tdesvn-e2f541c98dfa4081fa3ab3d28f08ea2309281884.zip
Added KDE3 version of kdesvn
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kdesvn@1103685 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/helpers')
-rw-r--r--src/helpers/CMakeLists.txt14
-rw-r--r--src/helpers/ktranslateurl.cpp141
-rw-r--r--src/helpers/ktranslateurl.h43
-rw-r--r--src/helpers/sshagent.cpp257
-rw-r--r--src/helpers/sshagent.h65
-rw-r--r--src/helpers/stringhelper.h66
-rw-r--r--src/helpers/sub2qt.cpp49
-rw-r--r--src/helpers/sub2qt.h43
8 files changed, 678 insertions, 0 deletions
diff --git a/src/helpers/CMakeLists.txt b/src/helpers/CMakeLists.txt
new file mode 100644
index 0000000..c9b0051
--- /dev/null
+++ b/src/helpers/CMakeLists.txt
@@ -0,0 +1,14 @@
+SET(helperssrc
+ ktranslateurl.cpp
+ sshagent.cpp
+ sub2qt.cpp)
+
+FILE(GLOB hdr RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.h")
+
+KDE3_AUTOMOC(${helperssrc} )
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
+ADD_LIBRARY(kdesvnhelpers STATIC ${helperssrc} ${hdr})
+
+SET_TARGET_PROPERTIES(kdesvnhelpers
+ PROPERTIES
+ COMPILE_FLAGS ${CMAKE_SHARED_LIBRARY_CXX_FLAGS})
diff --git a/src/helpers/ktranslateurl.cpp b/src/helpers/ktranslateurl.cpp
new file mode 100644
index 0000000..8c5e903
--- /dev/null
+++ b/src/helpers/ktranslateurl.cpp
@@ -0,0 +1,141 @@
+/***************************************************************************
+ * Copyright (C) 2005-2007 by Rajko Albrecht *
+ * ral@alwins-world.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#include "ktranslateurl.h"
+
+#include <kglobal.h>
+#include <kstandarddirs.h>
+#include <kdebug.h>
+#include <kfileitem.h>
+#include <kdesktopfile.h>
+
+#include <qstringlist.h>
+#include <qdir.h>
+
+namespace helpers {
+
+KTranslateUrl::KTranslateUrl()
+{
+}
+
+
+KTranslateUrl::~KTranslateUrl()
+{
+}
+
+KURL KTranslateUrl::translateSystemUrl(const KURL&_url)
+{
+ QString proto = _url.protocol();
+ KURL res;
+ QString name,path;
+
+ if (proto!="system") {
+ return _url;
+ }
+ KGlobal::dirs()->addResourceType("system_entries",
+ KStandardDirs::kde_default("data") + "systemview");
+ QStringList dirList = KGlobal::dirs()->resourceDirs("system_entries");
+ if (!parseURL(_url,name,path)) {
+ return _url;
+ }
+ res = findSystemBase(name);
+ if (!res.isValid()) {
+ return _url;
+ }
+ res.addPath(path);
+ res.setQuery(_url.query());
+ return res;
+}
+
+bool KTranslateUrl::parseURL(const KURL&url,QString&name,QString&path)
+{
+ QString url_path = url.path();
+ int i = url_path.find('/', 1);
+ if (i > 0)
+ {
+ name = url_path.mid(1, i-1);
+ path = url_path.mid(i+1);
+ }
+ else
+ {
+ name = url_path.mid(1);
+ path = QString::null;
+ }
+
+ return name != QString::null;
+}
+
+KURL KTranslateUrl::findSystemBase(const QString&filename)
+{
+ QStringList dirList = KGlobal::dirs()->resourceDirs("system_entries");
+
+ QStringList::ConstIterator dirpath = dirList.begin();
+ QStringList::ConstIterator end = dirList.end();
+ for(; dirpath!=end; ++dirpath)
+ {
+ QDir dir = *dirpath;
+ if (!dir.exists()) continue;
+
+ QStringList filenames
+ = dir.entryList( QDir::Files | QDir::Readable );
+
+
+ KIO::UDSEntry entry;
+
+ QStringList::ConstIterator name = filenames.begin();
+ QStringList::ConstIterator endf = filenames.end();
+
+ for(; name!=endf; ++name)
+ {
+ if (*name==filename+".desktop")
+ {
+ KDesktopFile desktop(*dirpath+filename+".desktop", true);
+ if ( desktop.readURL().isEmpty() )
+ {
+ KURL url;
+ url.setPath( desktop.readPath() );
+ return url;
+ }
+
+ return desktop.readURL();
+ }
+ }
+ }
+
+ return KURL();
+}
+
+}
+
+
+/*!
+ \fn helpers::KTranslateUrl::makeKdeUrl(const QString&inUrl)
+ */
+QString helpers::KTranslateUrl::makeKdeUrl(const QString&_proto)
+{
+ QString proto;
+ if (_proto.startsWith("svn+")){
+ proto = "k"+_proto;
+ } else if (_proto== QString("svn")){
+ proto = "ksvn";
+ } else {
+ proto = "ksvn+"+_proto;
+ }
+ return proto;
+}
diff --git a/src/helpers/ktranslateurl.h b/src/helpers/ktranslateurl.h
new file mode 100644
index 0000000..bad6a6a
--- /dev/null
+++ b/src/helpers/ktranslateurl.h
@@ -0,0 +1,43 @@
+/***************************************************************************
+ * Copyright (C) 2005-2007 by Rajko Albrecht *
+ * ral@alwins-world.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#ifndef HELPERSKTRANSLATEURL_H
+#define HELPERSKTRANSLATEURL_H
+
+#include <kurl.h>
+
+namespace helpers {
+
+/**
+ @author Rajko Albrecht <ral@alwins-world.de>
+*/
+class KTranslateUrl{
+public:
+ KTranslateUrl();
+ ~KTranslateUrl();
+
+ static KURL translateSystemUrl(const KURL&);
+ static bool parseURL(const KURL&,QString&name,QString&path);
+ static KURL findSystemBase(const QString&name);
+ static QString makeKdeUrl(const QString&inUrl);
+};
+
+}
+
+#endif
diff --git a/src/helpers/sshagent.cpp b/src/helpers/sshagent.cpp
new file mode 100644
index 0000000..ccde212
--- /dev/null
+++ b/src/helpers/sshagent.cpp
@@ -0,0 +1,257 @@
+/*
+ * Copyright (c) 2003 Christian Loose <christian.loose@hamburg.de>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "sshagent.h"
+#include "kdesvn-config.h"
+
+#include <qregexp.h>
+#include <kapplication.h>
+#include <kdeversion.h>
+#include <kprocess.h>
+#include <kdebug.h>
+
+#include <stdlib.h>
+
+
+// initialize static member variables
+bool SshAgent::m_isRunning = false;
+bool SshAgent::m_isOurAgent = false;
+bool SshAgent::m_addIdentitiesDone = false;
+QString SshAgent::m_authSock = QString::null;
+QString SshAgent::m_pid = QString::null;
+
+
+SshAgent::SshAgent(QObject* parent, const char* name)
+ : QObject(parent, name)
+{
+}
+
+
+SshAgent::~SshAgent()
+{
+}
+
+
+bool SshAgent::querySshAgent()
+{
+ if( m_isRunning )
+ return true;
+
+ // Did the user already start a ssh-agent process?
+ char* pid;
+ if( (pid = ::getenv("SSH_AGENT_PID")) != 0 )
+ {
+ m_pid = QString::fromLocal8Bit(pid);
+
+ char* sock = ::getenv("SSH_AUTH_SOCK");
+ if( sock )
+ m_authSock = QString::fromLocal8Bit(sock);
+ /* make sure that we have a askpass program.
+ * on some systems something like that isn't installed.*/
+#ifdef FORCE_ASKPASS
+ kdDebug()<<"Using test askpass"<<endl;
+#ifdef HAS_SETENV
+ ::setenv("SSH_ASKPASS",FORCE_ASKPASS,1);
+#else
+ ::putenv("SSH_ASKPASS="FORCE_ASKPASS);
+#endif
+#else
+/*
+ char*agent = ::getenv("SSH_ASKPASS");
+ if (!agent) {
+*/
+#ifdef HAS_SETENV
+ ::setenv("SSH_ASKPASS", "kdesvnaskpass",1);
+#else
+ ::putenv("SSH_ASKPASS=kdesvnaskpass");
+#endif
+/*
+ }
+*/
+#endif
+ m_isOurAgent = false;
+ m_isRunning = true;
+ }
+ // We have to start a new ssh-agent process
+ else
+ {
+ m_isOurAgent = true;
+ m_isRunning = startSshAgent();
+ }
+
+ return m_isRunning;
+}
+
+
+bool SshAgent::addSshIdentities(bool force)
+{
+ if (m_addIdentitiesDone && !force) {
+ return true;
+ }
+
+
+ if( !m_isRunning || (!m_isOurAgent&&!force)) {
+ return false;
+ }
+
+ // add identities to ssh-agent
+ KProcess proc;
+
+ proc.setEnvironment("SSH_AGENT_PID", m_pid);
+ proc.setEnvironment("SSH_AUTH_SOCK", m_authSock);
+
+#ifdef FORCE_ASKPASS
+ kdDebug()<<"Using test askpass"<<endl;
+ proc.setEnvironment("SSH_ASKPASS",FORCE_ASKPASS);
+#else
+ char*agent = 0;
+/*
+ if (force) {
+ agent = ::getenv("SSH_ASKPASS");
+ }
+*/
+ if (!agent) {
+ proc.setEnvironment("SSH_ASKPASS", "kdesvnaskpass");
+ }
+#endif
+
+ proc << "ssh-add";
+
+ connect(&proc, SIGNAL(receivedStdout(KProcess*, char*, int)),
+ SLOT(slotReceivedStdout(KProcess*, char*, int)));
+ connect(&proc, SIGNAL(receivedStderr(KProcess*, char*, int)),
+ SLOT(slotReceivedStderr(KProcess*, char*, int)));
+
+ proc.start(KProcess::DontCare, KProcess::AllOutput);
+
+ // wait for process to finish
+ // TODO CL use timeout?
+ proc.wait();
+
+ m_addIdentitiesDone = proc.normalExit() && proc.exitStatus() == 0;
+ return m_addIdentitiesDone;
+}
+
+
+void SshAgent::killSshAgent()
+{
+ if( !m_isRunning || !m_isOurAgent )
+ return;
+
+ KProcess proc;
+
+ proc << "kill" << m_pid;
+
+ proc.start(KProcess::DontCare, KProcess::NoCommunication);
+}
+
+
+void SshAgent::slotProcessExited(KProcess*)
+{
+ QRegExp cshPidRx("setenv SSH_AGENT_PID (\\d*);");
+ QRegExp cshSockRx("setenv SSH_AUTH_SOCK (.*);");
+
+ QRegExp bashPidRx("SSH_AGENT_PID=(\\d*).*");
+ QRegExp bashSockRx("SSH_AUTH_SOCK=(.*\\.\\d*);.*");
+ QStringList m_outputLines = QStringList::split("\n",m_Output);
+
+ QStringList::Iterator it = m_outputLines.begin();
+ QStringList::Iterator end = m_outputLines.end();
+ for( ; it != end; ++it )
+ {
+ if( m_pid.isEmpty() )
+ {
+ int pos = cshPidRx.search(*it);
+ if( pos > -1 )
+ {
+ m_pid = cshPidRx.cap(1);
+ continue;
+ }
+
+ pos = bashPidRx.search(*it);
+ if( pos > -1 )
+ {
+ m_pid = bashPidRx.cap(1);
+ continue;
+ }
+ }
+
+ if( m_authSock.isEmpty() )
+ {
+ int pos = cshSockRx.search(*it);
+ if( pos > -1 )
+ {
+ m_authSock = cshSockRx.cap(1);
+ continue;
+ }
+
+ pos = bashSockRx.search(*it);
+ if( pos > -1 )
+ {
+ m_authSock = bashSockRx.cap(1);
+ continue;
+ }
+ }
+ }
+
+}
+
+
+void SshAgent::slotReceivedStdout(KProcess* proc, char* buffer, int buflen)
+{
+ Q_UNUSED(proc);
+
+ QString output = QString::fromLocal8Bit(buffer, buflen);
+ m_Output+=output;
+}
+
+
+void SshAgent::slotReceivedStderr(KProcess* proc, char* buffer, int buflen)
+{
+ Q_UNUSED(proc);
+
+ QString output = QString::fromLocal8Bit(buffer, buflen);
+ m_Output+=output;
+}
+
+
+bool SshAgent::startSshAgent()
+{
+ KProcess proc;
+
+ proc << "ssh-agent";
+
+ connect(&proc, SIGNAL(processExited(KProcess*)),
+ SLOT(slotProcessExited(KProcess*)));
+ connect(&proc, SIGNAL(receivedStdout(KProcess*, char*, int)),
+ SLOT(slotReceivedStdout(KProcess*, char*, int)));
+ connect(&proc, SIGNAL(receivedStderr(KProcess*, char*, int)),
+ SLOT(slotReceivedStderr(KProcess*, char*, int)) );
+
+ proc.start(KProcess::NotifyOnExit, KProcess::All);
+
+ // wait for process to finish
+ // TODO CL use timeout?
+ proc.wait();
+
+ return (proc.normalExit() && proc.exitStatus() == 0);
+}
+
+#include "sshagent.moc"
diff --git a/src/helpers/sshagent.h b/src/helpers/sshagent.h
new file mode 100644
index 0000000..f5738fb
--- /dev/null
+++ b/src/helpers/sshagent.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2003 Christian Loose <christian.loose@hamburg.de>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef SSHAGENT_H
+#define SSHAGENT_H
+
+#include <qobject.h>
+#include <qstring.h>
+#include <qstringlist.h>
+
+class KProcess;
+
+
+class SshAgent : public QObject
+{
+ Q_OBJECT
+
+public:
+ SshAgent(QObject* parent = 0, const char* name = 0);
+ ~SshAgent();
+
+ bool querySshAgent();
+ bool addSshIdentities(bool force=false);
+ void killSshAgent();
+
+ bool isRunning() const { return m_isRunning; }
+ QString pid() const { return m_pid; }
+ QString authSock() const { return m_authSock; }
+
+private slots:
+ void slotProcessExited(KProcess*);
+ void slotReceivedStdout(KProcess* proc, char* buffer, int buflen);
+ void slotReceivedStderr(KProcess* proc, char* buffer, int buflen);
+
+private:
+ bool startSshAgent();
+
+ QString m_Output;
+
+ static bool m_isRunning;
+ static bool m_isOurAgent;
+ static bool m_addIdentitiesDone;
+ static QString m_authSock;
+ static QString m_pid;
+};
+
+
+#endif
diff --git a/src/helpers/stringhelper.h b/src/helpers/stringhelper.h
new file mode 100644
index 0000000..263f418
--- /dev/null
+++ b/src/helpers/stringhelper.h
@@ -0,0 +1,66 @@
+/***************************************************************************
+ * Copyright (C) 2006-2007 by Rajko Albrecht *
+ * ral@alwins-world.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#ifndef STRINGHELPER_H
+#define STRINGHELPER_H
+
+#include <qstring.h>
+#include <qtextstream.h>
+
+namespace helpers
+{
+
+class ByteToString
+{
+protected:
+
+public:
+ ByteToString(){};
+
+ QString operator()(long value)
+ {
+ char pre = 0;
+ double v = (double)value;
+ if (v<0) v=0;
+ while (v>=1024.0 && pre != 'T')
+ {
+ switch (pre)
+ {
+ case 'k':
+ pre = 'M';
+ break;
+ case 'M':
+ pre = 'G';
+ break;
+ case 'G':
+ pre = 'T';
+ break;
+ default:
+ pre = 'k';
+ break;
+ }
+ v /= 1024.0;
+ }
+ return QString("%1 %2Byte").arg(v,0,'f',pre?2:0).arg(pre?QString(QChar(pre)):QString(""));
+ }
+};
+
+}
+
+#endif
diff --git a/src/helpers/sub2qt.cpp b/src/helpers/sub2qt.cpp
new file mode 100644
index 0000000..1ca0c28
--- /dev/null
+++ b/src/helpers/sub2qt.cpp
@@ -0,0 +1,49 @@
+/***************************************************************************
+ * Copyright (C) 2005-2007 by Rajko Albrecht *
+ * ral@alwins-world.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#include "sub2qt.h"
+
+#include "src/svnqt/datetime.hpp"
+#include "kglobal.h"
+#include "klocale.h"
+#include <qmap.h>
+
+namespace helpers {
+
+sub2qt::sub2qt()
+{
+}
+
+
+sub2qt::~sub2qt()
+{
+}
+
+QString sub2qt::apr_time2qtString(apr_time_t _time)
+{
+ return DateTime2qtString(_time);
+}
+
+QString sub2qt::DateTime2qtString(const svn::DateTime&_time)
+{
+ return KGlobal::locale()->formatDateTime(_time);
+}
+
+};
+
diff --git a/src/helpers/sub2qt.h b/src/helpers/sub2qt.h
new file mode 100644
index 0000000..23eda5f
--- /dev/null
+++ b/src/helpers/sub2qt.h
@@ -0,0 +1,43 @@
+/***************************************************************************
+ * Copyright (C) 2005-2007 by Rajko Albrecht *
+ * ral@alwins-world.de *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#ifndef HELPERSSUB2QT_H
+#define HELPERSSUB2QT_H
+
+#include "src/svnqt/datetime.hpp"
+#include <qdatetime.h>
+#include <qstring.h>
+#include <svn_time.h>
+
+namespace helpers {
+
+/**
+@author Rajko Albrecht
+*/
+class sub2qt{
+public:
+ sub2qt();
+ ~sub2qt();
+
+ static QString apr_time2qtString(apr_time_t _time);
+ static QString DateTime2qtString(const svn::DateTime&_time);
+};
+
+}
+#endif