File indexing completed on 2024-05-19 04:45:33

0001 /******************************************************************************
0002  * This file is part of the libqgit2 library
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017  */
0018 
0019 #include "remotecallbacks.h"
0020 #include "qgitcredentials.h"
0021 #include "credentials_p.h"
0022 
0023 namespace LibQGit2
0024 {
0025 namespace internal
0026 {
0027 
0028 
0029 RemoteListener::~RemoteListener() {}
0030 
0031 
0032 RemoteCallbacks::RemoteCallbacks(RemoteListener *listener, const Credentials &credentials) :
0033     m_listener(listener),
0034     m_credentials(credentials),
0035     m_transferProgress(-1)
0036 {
0037 }
0038 
0039 git_remote_callbacks RemoteCallbacks::rawCallbacks() const
0040 {
0041     git_remote_callbacks remoteCallbacks = GIT_REMOTE_CALLBACKS_INIT;
0042     remoteCallbacks.payload = (void*)this;
0043 
0044     if (m_listener) {
0045         remoteCallbacks.transfer_progress = &transferProgressCallback;
0046     }
0047 
0048     if (!m_credentials.isEmpty()) {
0049         remoteCallbacks.credentials = &acquireCredentialsCallback;
0050     }
0051 
0052     return remoteCallbacks;
0053 }
0054 
0055 int RemoteCallbacks::transferProgressCallback(const git_transfer_progress* stats, void* data)
0056 {
0057     int ret = 0;
0058 
0059     if (data && stats) {
0060         RemoteCallbacks* cb = static_cast<RemoteCallbacks*>(data);
0061 
0062         int percent = (int)(0.5 + 100.0 * ((double)stats->received_objects) / ((double)stats->total_objects));
0063         if (percent != cb->m_transferProgress) {
0064             cb->m_transferProgress = percent;
0065             ret = cb->m_listener->progress(percent);
0066         }
0067     }
0068 
0069     return ret;
0070 }
0071 
0072 int RemoteCallbacks::acquireCredentialsCallback(git_credential **cred, const char *url, const char *usernameFromUrl, unsigned int allowedTypes, void *data)
0073 {
0074     int result = -1;
0075     if (data) {
0076         Credentials &credentials = static_cast<RemoteCallbacks*>(data)->m_credentials;
0077         result = CredentialsPrivate::create(credentials, cred, url, usernameFromUrl, allowedTypes);
0078     }
0079 
0080     return result;
0081 }
0082 
0083 }
0084 }