| 1 | /* |
| 2 | * Copyright (c) 2002-2004 Christian Loose <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "repository.h" |
| 22 | |
| 23 | #include <QDBusConnection> |
| 24 | #include <QStandardPaths> |
| 25 | #include <QTextStream> |
| 26 | #include <qdir.h> |
| 27 | #include <qfile.h> |
| 28 | #include <qstring.h> |
| 29 | |
| 30 | #include <kconfiggroup.h> |
| 31 | #include <kdirwatch.h> |
| 32 | #include <ksharedconfig.h> |
| 33 | |
| 34 | #include "sshagent.h" |
| 35 | #include <repositoryadaptor.h> |
| 36 | |
| 37 | struct Repository::Private { |
| 38 | Private() |
| 39 | : compressionLevel(0) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | QString configFileName; |
| 44 | |
| 45 | QString workingCopy; |
| 46 | QString location; |
| 47 | |
| 48 | QString client; |
| 49 | QString rsh; |
| 50 | QString server; |
| 51 | int compressionLevel; |
| 52 | bool retrieveCvsignoreFile; |
| 53 | |
| 54 | void readConfig(); |
| 55 | void readGeneralConfig(); |
| 56 | }; |
| 57 | |
| 58 | Repository::Repository() |
| 59 | : QObject() |
| 60 | , d(new Private) |
| 61 | { |
| 62 | d->readGeneralConfig(); |
| 63 | new RepositoryAdaptor(this); |
| 64 | QDBusConnection::sessionBus().registerObject("/CvsRepository", this); |
| 65 | |
| 66 | // other cvsservice instances might change the configuration file |
| 67 | // so we watch it for changes |
| 68 | d->configFileName = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, "cvsservicerc"); |
| 69 | auto fileWatcher = new KDirWatch(this); |
| 70 | connect(fileWatcher, SIGNAL(dirty(QString)), this, SLOT(slotConfigDirty(QString))); |
| 71 | fileWatcher->addFile(d->configFileName); |
| 72 | } |
| 73 | |
| 74 | Repository::Repository(const QString &repository) |
| 75 | : QObject() |
| 76 | , d(new Private) |
| 77 | { |
| 78 | d->location = repository; |
| 79 | d->readGeneralConfig(); |
| 80 | d->readConfig(); |
| 81 | // TODO verify it before : DCOPObject() |
| 82 | new RepositoryAdaptor(this); |
| 83 | QDBusConnection::sessionBus().registerObject("/CvsRepository", this); |
| 84 | |
| 85 | // other cvsservice instances might change the configuration file |
| 86 | // so we watch it for changes |
| 87 | d->configFileName = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, "cvsservicerc"); |
| 88 | auto fileWatcher = new KDirWatch(this); |
| 89 | connect(fileWatcher, SIGNAL(dirty(QString)), this, SLOT(slotConfigDirty(QString))); |
| 90 | fileWatcher->addFile(d->configFileName); |
| 91 | } |
| 92 | |
| 93 | Repository::~Repository() |
| 94 | { |
| 95 | delete d; |
| 96 | } |
| 97 | |
| 98 | QString Repository::cvsClient() const |
| 99 | { |
| 100 | QString client(d->client); |
| 101 | |
| 102 | // suppress reading of the '.cvsrc' file |
| 103 | client += " -f"; |
| 104 | |
| 105 | // we don't need the command line option if there is no compression level set |
| 106 | if (d->compressionLevel > 0) { |
| 107 | client += " -z" + QString::number(d->compressionLevel) + ' '; |
| 108 | } |
| 109 | |
| 110 | return client; |
| 111 | } |
| 112 | |
| 113 | QString Repository::clientOnly() const |
| 114 | { |
| 115 | return d->client; |
| 116 | } |
| 117 | |
| 118 | QString Repository::rsh() const |
| 119 | { |
| 120 | return d->rsh; |
| 121 | } |
| 122 | |
| 123 | QString Repository::server() const |
| 124 | { |
| 125 | return d->server; |
| 126 | } |
| 127 | |
| 128 | bool Repository::setWorkingCopy(const QString &dirName) |
| 129 | { |
| 130 | const QFileInfo fi(dirName); |
| 131 | const QString path = fi.absoluteFilePath(); |
| 132 | |
| 133 | // is this really a cvs-controlled directory? |
| 134 | const QFileInfo cvsDirInfo(path + "/CVS"); |
| 135 | if (!cvsDirInfo.exists() || !cvsDirInfo.isDir() || !QFile::exists(cvsDirInfo.filePath() + "/Entries") |
| 136 | || !QFile::exists(cvsDirInfo.filePath() + "/Repository") || !QFile::exists(cvsDirInfo.filePath() + "/Root")) |
| 137 | return false; |
| 138 | |
| 139 | d->workingCopy = path; |
| 140 | d->location.clear(); |
| 141 | |
| 142 | // determine path to the repository |
| 143 | QFile rootFile(path + "/CVS/Root"); |
| 144 | if (rootFile.open(QIODevice::ReadOnly)) { |
| 145 | QTextStream stream(&rootFile); |
| 146 | d->location = stream.readLine(); |
| 147 | } |
| 148 | rootFile.close(); |
| 149 | |
| 150 | // add identities (ssh-add) to ssh-agent |
| 151 | // TODO CL make sure this is called only once |
| 152 | if (d->location.contains(":ext:", Qt::CaseInsensitive)) { |
| 153 | SshAgent ssh; |
| 154 | ssh.addSshIdentities(); |
| 155 | } |
| 156 | |
| 157 | QDir::setCurrent(path); |
| 158 | d->readConfig(); |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | QString Repository::workingCopy() const |
| 164 | { |
| 165 | return d->workingCopy; |
| 166 | } |
| 167 | |
| 168 | QString Repository::location() const |
| 169 | { |
| 170 | return d->location; |
| 171 | } |
| 172 | |
| 173 | bool Repository::retrieveCvsignoreFile() const |
| 174 | { |
| 175 | return d->retrieveCvsignoreFile; |
| 176 | } |
| 177 | |
| 178 | void Repository::slotConfigDirty(const QString &fileName) |
| 179 | { |
| 180 | if (fileName == d->configFileName) { |
| 181 | // reread the configuration data from disk |
| 182 | KSharedConfig::openConfig()->reparseConfiguration(); |
| 183 | d->readConfig(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void Repository::Private::readGeneralConfig() |
| 188 | { |
| 189 | // get path to cvs client program |
| 190 | KConfigGroup cg(KSharedConfig::openConfig(), "General"); |
| 191 | client = cg.readPathEntry("CVSPath", "cvs"); |
| 192 | } |
| 193 | |
| 194 | void Repository::Private::readConfig() |
| 195 | { |
| 196 | KSharedConfig::Ptr config = KSharedConfig::openConfig(); |
| 197 | |
| 198 | // Sometimes the location can be unequal to the entry in the CVS/Root. |
| 199 | // |
| 200 | // This can happen when the checkout was done with a repository name |
| 201 | // like :pserver:[email protected]:/home/kde. When cvs then saves the |
| 202 | // name into the .cvspass file, it adds the default cvs port to it like |
| 203 | // this :pserver:[email protected]:2401/home/kde. This name is then also |
| 204 | // used for the configuration group. |
| 205 | // |
| 206 | // In order to be able to read this group, we then have to manually add |
| 207 | // the port number to it. |
| 208 | QString repositoryGroup = QLatin1String("Repository-") + location; |
| 209 | if (!config->hasGroup(repositoryGroup)) { |
| 210 | // find the position of the first path separator |
| 211 | const int insertPos = repositoryGroup.indexOf('/'); |
| 212 | if (insertPos > 0) { |
| 213 | // add port to location |
| 214 | // (1) :pserver:[email protected]:/path |
| 215 | if (repositoryGroup.at(insertPos - 1) == ':') |
| 216 | repositoryGroup.insert(insertPos, "2401"); |
| 217 | // (2) :pserver:[email protected]/path |
| 218 | else |
| 219 | repositoryGroup.insert(insertPos, ":2401"); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | KConfigGroup group = config->group(repositoryGroup); |
| 224 | |
| 225 | // should we retrieve the CVSROOT/cvsignore file from the cvs server? |
| 226 | retrieveCvsignoreFile = group.readEntry("RetrieveCvsignore", false); |
| 227 | |
| 228 | // see if there is a specific compression level set for this repository |
| 229 | compressionLevel = group.readEntry("Compression", -1); |
| 230 | |
| 231 | // use default global compression level instead? |
| 232 | if (compressionLevel < 0) { |
| 233 | KConfigGroup cs(config, "General"); |
| 234 | compressionLevel = cs.readEntry("Compression", 0); |
| 235 | } |
| 236 | |
| 237 | // get remote shell client to access the remote repository |
| 238 | rsh = group.readPathEntry("rsh", QString()); |
| 239 | |
| 240 | // get program to start on the server side |
| 241 | server = group.readEntry("cvs_server"); |
| 242 | } |