File indexing completed on 2024-05-12 05:44:24

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "tcontextlistener.h"
0021 
0022 #include "helpers/stringhelper.h"
0023 #include "ksvnwidgets/commitmsg_impl.h"
0024 #include "svnfrontend/ccontextlistener.h"
0025 #include "threadcontextlistenerdata.h"
0026 
0027 #include <KLocalizedString>
0028 
0029 // ThreadContextListenerData
0030 QMutex *ThreadContextListenerData::callbackMutex()
0031 {
0032     static QMutex s_CallbackMutex;
0033     return &s_CallbackMutex;
0034 }
0035 
0036 // ThreadContextListener
0037 ThreadContextListener::ThreadContextListener(QObject *parent)
0038     : CContextListener(parent)
0039     , m_Data(new ThreadContextListenerData)
0040 {
0041     connect(this, &ThreadContextListener::signal_contextGetLogin, this, &ThreadContextListener::event_contextGetLogin, Qt::BlockingQueuedConnection);
0042     connect(this, &ThreadContextListener::signal_contextGetSavedLogin, this, &ThreadContextListener::event_contextGetSavedLogin, Qt::BlockingQueuedConnection);
0043     connect(this, &ThreadContextListener::signal_contextGetLogMessage, this, &ThreadContextListener::event_contextGetLogMessage, Qt::BlockingQueuedConnection);
0044     connect(this,
0045             &ThreadContextListener::signal_contextSslClientCertPrompt,
0046             this,
0047             &ThreadContextListener::event_contextSslClientCertPrompt,
0048             Qt::BlockingQueuedConnection);
0049     connect(this,
0050             &ThreadContextListener::signal_contextSslClientCertPwPrompt,
0051             this,
0052             &ThreadContextListener::event_contextSslClientCertPwPrompt,
0053             Qt::BlockingQueuedConnection);
0054     connect(this,
0055             &ThreadContextListener::signal_contextSslServerTrustPrompt,
0056             this,
0057             &ThreadContextListener::event_contextSslServerTrustPrompt,
0058             Qt::BlockingQueuedConnection);
0059     // no user input, BlockingQueuedConnection not needed here
0060     connect(this, &ThreadContextListener::signal_contextNotify, this, &ThreadContextListener::event_contextNotify);
0061 }
0062 
0063 ThreadContextListener::~ThreadContextListener()
0064 {
0065     delete m_Data;
0066 }
0067 
0068 bool ThreadContextListener::contextGetLogin(const QString &realm, QString &username, QString &password, bool &maySave)
0069 {
0070     QMutexLocker lock(ThreadContextListenerData::callbackMutex());
0071 
0072     m_Data->m_slogin_data.realm = realm;
0073     m_Data->m_slogin_data.user = username;
0074     m_Data->m_slogin_data.password = password;
0075     m_Data->m_slogin_data.maysave = maySave;
0076     m_Data->bReturnValue = false;
0077 
0078     // call event_contextGetLogin() in main thread, wait until finished due to BlockingQueuedConnection
0079     emit signal_contextGetLogin();
0080 
0081     username = m_Data->m_slogin_data.user;
0082     password = m_Data->m_slogin_data.password;
0083     maySave = m_Data->m_slogin_data.maysave;
0084     return m_Data->bReturnValue;
0085 }
0086 
0087 bool ThreadContextListener::contextGetSavedLogin(const QString &realm, QString &username, QString &password)
0088 {
0089     QMutexLocker lock(ThreadContextListenerData::callbackMutex());
0090 
0091     m_Data->m_slogin_data.realm = realm;
0092     m_Data->m_slogin_data.user = username;
0093     m_Data->m_slogin_data.password = password;
0094     m_Data->m_slogin_data.maysave = false;
0095     m_Data->bReturnValue = false;
0096 
0097     // call event_contextGetSavedLogin() in main thread, wait until finished due to BlockingQueuedConnection
0098     emit signal_contextGetSavedLogin();
0099 
0100     username = m_Data->m_slogin_data.user;
0101     password = m_Data->m_slogin_data.password;
0102     return m_Data->bReturnValue;
0103 }
0104 
0105 bool ThreadContextListener::contextGetLogMessage(QString &msg, const svn::CommitItemList &_items)
0106 {
0107     QMutexLocker lock(ThreadContextListenerData::callbackMutex());
0108 
0109     m_Data->m_slog_message.items = _items;
0110     m_Data->bReturnValue = false;
0111 
0112     // call event_contextGetLogMessage() in main thread, wait until finished due to BlockingQueuedConnection
0113     emit signal_contextGetLogMessage();
0114 
0115     msg = m_Data->m_slog_message.msg;
0116     return m_Data->bReturnValue;
0117 }
0118 
0119 bool ThreadContextListener::contextSslClientCertPrompt(QString &certFile)
0120 {
0121     QMutexLocker lock(ThreadContextListenerData::callbackMutex());
0122 
0123     m_Data->m_scert_file.certfile.clear();
0124     m_Data->bReturnValue = false;
0125 
0126     // call event_contextSslClientCertPrompt() in main thread, wait until finished due to BlockingQueuedConnection
0127     emit signal_contextSslClientCertPrompt();
0128 
0129     certFile = m_Data->m_scert_file.certfile;
0130     return m_Data->bReturnValue;
0131 }
0132 
0133 bool ThreadContextListener::contextSslClientCertPwPrompt(QString &password, const QString &realm, bool &maySave)
0134 {
0135     QMutexLocker lock(ThreadContextListenerData::callbackMutex());
0136 
0137     m_Data->m_scert_pw.maysave = false;
0138     m_Data->m_scert_pw.realm = realm;
0139     m_Data->bReturnValue = false;
0140 
0141     // call event_contextSslClientCertPrompt() in main thread, wait until finished due to BlockingQueuedConnection
0142     emit signal_contextSslClientCertPwPrompt();
0143 
0144     password = m_Data->m_scert_pw.password;
0145     maySave = m_Data->m_scert_pw.maysave;
0146     return m_Data->bReturnValue;
0147 }
0148 
0149 svn::ContextListener::SslServerTrustAnswer ThreadContextListener::contextSslServerTrustPrompt(const SslServerTrustData &data,
0150                                                                                               apr_uint32_t & /* acceptedFailures*/)
0151 {
0152     QMutexLocker lock(ThreadContextListenerData::callbackMutex());
0153 
0154     m_Data->m_strust_answer.sslTrustAnswer = DONT_ACCEPT;
0155     m_Data->m_strust_answer.trustdata = data;
0156     m_Data->bReturnValue = false;
0157 
0158     // call event_contextSslClientCertPrompt() in main thread, wait until finished due to BlockingQueuedConnection
0159     emit signal_contextSslServerTrustPrompt();
0160 
0161     return m_Data->m_strust_answer.sslTrustAnswer;
0162 }
0163 
0164 void ThreadContextListener::contextNotify(const QString &aMsg)
0165 {
0166     // call event_contextNotify() in main thread
0167     emit signal_contextNotify(aMsg);
0168 }
0169 
0170 /*!
0171     \fn ThreadContextListener::contextProgress(long long int current, long long int max)
0172  */
0173 void ThreadContextListener::contextProgress(long long int current, long long int max)
0174 {
0175     if (m_Data->noProgress || current == 0) {
0176         return;
0177     }
0178     QString msg;
0179     QString s1 = helpers::ByteToString(current);
0180     if (max > -1) {
0181         QString s2 = helpers::ByteToString(max);
0182         msg = i18n("%1 of %2 transferred.", s1, s2);
0183     } else {
0184         msg = i18n("%1 transferred.", s1);
0185     }
0186     emit signal_contextNotify(msg);
0187 }
0188 
0189 void ThreadContextListener::sendTick()
0190 {
0191     emit signal_contextNotify(QString());
0192 }
0193 
0194 /* methods below may only called from mainthread! (via signal/slot) */
0195 void ThreadContextListener::event_contextGetLogin()
0196 {
0197     m_Data->bReturnValue = CContextListener::contextGetLogin(m_Data->m_slogin_data.realm,
0198                                                              m_Data->m_slogin_data.user,
0199                                                              m_Data->m_slogin_data.password,
0200                                                              m_Data->m_slogin_data.maysave);
0201 }
0202 
0203 void ThreadContextListener::event_contextGetSavedLogin()
0204 {
0205     m_Data->bReturnValue = CContextListener::contextGetSavedLogin(m_Data->m_slogin_data.realm, m_Data->m_slogin_data.user, m_Data->m_slogin_data.password);
0206 }
0207 
0208 void ThreadContextListener::event_contextGetLogMessage()
0209 {
0210     m_Data->bReturnValue = CContextListener::contextGetLogMessage(m_Data->m_slog_message.msg, m_Data->m_slog_message.items);
0211 }
0212 
0213 void ThreadContextListener::event_contextSslClientCertPrompt()
0214 {
0215     m_Data->bReturnValue = CContextListener::contextSslClientCertPrompt(m_Data->m_scert_file.certfile);
0216 }
0217 
0218 void ThreadContextListener::event_contextSslClientCertPwPrompt()
0219 {
0220     m_Data->bReturnValue = CContextListener::contextSslClientCertPwPrompt(m_Data->m_scert_pw.password, m_Data->m_scert_pw.realm, m_Data->m_scert_pw.maysave);
0221 }
0222 
0223 void ThreadContextListener::event_contextSslServerTrustPrompt()
0224 {
0225     m_Data->m_strust_answer.sslTrustAnswer =
0226         CContextListener::contextSslServerTrustPrompt(m_Data->m_strust_answer.trustdata, m_Data->m_strust_answer.trustdata.failures);
0227 }
0228 
0229 void ThreadContextListener::event_contextNotify(const QString &msg)
0230 {
0231     CContextListener::contextNotify(msg);
0232 }
0233 
0234 #include "moc_tcontextlistener.cpp"