summaryrefslogtreecommitdiffstats
path: root/cervisia/misc.cpp
blob: 9de5006f9e5470e06ab2f34b306bb1ba1025993c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 *  Copyright (C) 1999-2002 Bernd Gehrmann
 *                          bernd@mail.berlios.de
 *  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 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 "misc.h"

#include <ctype.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqregexp.h>
#include <tqstringlist.h>
#include <tdeconfig.h>
#include <tdeemailsettings.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kprocess.h>
#include <tdetempfile.h>
#include <kuser.h>
#include <kdebug.h>

#include "cvsservice_stub.h"
#include "progressdlg.h"

// These regular expression parts aren't useful to check the validity of the
// CVSROOT specification. They are just used to extract the different parts of it.
static const TQString userNameRegExp("([a-z0-9_][a-z0-9_-.]*)?");
static const TQString passwordRegExp("(:[^@]+)?");
static const TQString hostNameRegExp("([^:/@]+)");
static const TQString portRegExp("(:(\\d*))?");
static const TQString pathRegExp("(/.*)");


static int FindWhiteSpace(const TQString& str, int index)
{
    const int length = str.length();

    if( index < 0 )
        index += length;

    if( index < 0 || index >= length )
        return -1;

    const TQChar* const startPos = str.unicode();
    const TQChar* const endPos   = startPos + length;

    const TQChar* pos = startPos + index;
    while( pos < endPos && !pos->isSpace() )
        ++pos;

    const int foundIndex = pos - startPos;
    return (foundIndex < length ? foundIndex : -1);
}


static const TQStringList FetchBranchesAndTags(const TQString& searchedType,
                                              CvsService_stub* cvsService,
                                              TQWidget* parent)
{
    TQStringList branchOrTagList;

    DCOPRef job = cvsService->status(TQStringList(), true, true);
    if( !cvsService->ok() )
        return branchOrTagList;

    ProgressDialog dlg(parent, "Status", job, TQString(), i18n("CVS Status"));

    if( dlg.execute() )
    {
        TQString line;
        while( dlg.getLine(line) )
        {
            int wsPos, bracketPos, colonPos;

            if( line.isEmpty() || line[0] != '\t' )
                continue;
            if( (wsPos = FindWhiteSpace(line, 2)) < 0 )
                continue;
            if( (bracketPos = line.find('(', wsPos + 1)) < 0 )
                continue;
            if( (colonPos = line.find(':', bracketPos + 1)) < 0 )
                continue;

            const TQString tag  = line.mid(1, wsPos - 1);
            const TQString type = line.mid(bracketPos + 1, colonPos - bracketPos - 1);
            if( type == searchedType && !branchOrTagList.contains(tag) )
                branchOrTagList.push_back(tag);
        }

        branchOrTagList.sort();
    }

    return branchOrTagList;
}


bool Cervisia::IsValidTag(const TQString& tag)
{
    static const TQString prohibitedChars("$,.:;@");

    if( !isalpha(tag[0].latin1()) )
        return false;

    for( uint i = 1; i < tag.length(); ++i )
    {
        if( !isgraph(tag[i].latin1()) || prohibitedChars.contains(tag[i]) )
                return false;
    }

    return true;
}


TQString Cervisia::UserName()
{
    // 1. Try to retrieve the information from the control center settings
    KEMailSettings settings;
    TQString name  = settings.getSetting(KEMailSettings::RealName);
    TQString email = settings.getSetting(KEMailSettings::EmailAddress);

    if( name.isEmpty() || email.isEmpty() )
    {
        // 2. Try to retrieve the information from the system
        struct passwd* pw = getpwuid(getuid());
        if( !pw )
            return TQString();

        char hostname[512];
        hostname[0] = '\0';

        if( !gethostname(hostname, sizeof(hostname)) )
            hostname[sizeof(hostname)-1] = '0';

        name  = TQString::fromLocal8Bit(pw->pw_gecos);
        email = TQString::fromLocal8Bit(pw->pw_name) + "@" +
                TQString::fromLocal8Bit(hostname);
    }

    TQString result = name;
    result += "  <";
    result += email;
    result += ">";

    return result;
}


TQString Cervisia::NormalizeRepository(const TQString& repository)
{
    // only :pserver: repositories
    if( !repository.startsWith(":pserver:") )
        return repository;

    TQRegExp rx(":pserver:(" + userNameRegExp + passwordRegExp + "@)?" +
               hostNameRegExp + portRegExp + pathRegExp);

    // extract username, hostname, port and path from CVSROOT
    TQString userName, hostName, port, path;
    if( rx.search(repository) != -1 )
    {
        userName = rx.cap(2);
        hostName = rx.cap(4);
        port     = rx.cap(6);
        path     = rx.cap(7);

        kdDebug() << "NormalizeRepository(): username=" << userName << endl;
        kdDebug() << "NormalizeRepository(): hostname=" << hostName << endl;
        kdDebug() << "NormalizeRepository(): port    =" << port << endl;
        kdDebug() << "NormalizeRepository(): path    =" << path << endl;

        if( port.isEmpty() )
            port = "2401";

        if( userName.isEmpty() )
            userName = KUser().loginName();

        TQString canonicalForm = ":pserver:" + userName + "@" + hostName +
                                ":" + port + path;

        kdDebug() << "NormalizeRepository(): canonicalForm=" << canonicalForm
                  << endl;
        return canonicalForm;
    }
    else
        return repository;
}


bool Cervisia::CheckOverwrite(const TQString& fileName, TQWidget* parent)
{
    bool result = true;

    TQFileInfo fi(fileName);

    // does the file already exist?
    if( fi.exists() )
    {
        result = (KMessageBox::warningContinueCancel(parent,
                  i18n("A file named \"%1\" already exists. Are you sure you want to overwrite it?").arg(fileName),
                  i18n("Overwrite File?"),
                  KGuiItem(i18n("&Overwrite"), "document-save", i18n("Overwrite the file"))) == KMessageBox::Continue);
    }

    return result;
}


TQString joinLine(const TQStringList &list)
{
    TQString line;
    for ( TQStringList::ConstIterator it = list.begin();
          it != list.end(); ++it )
    {
        line += KShellProcess::quote(*it);
        line += " ";
    }

    if (line.length() > 0)
        line.truncate(line.length()-1);

    return line;
}


// Should be replaceable by TQStringList::split
TQStringList splitLine(TQString line, char delim)
{
    int pos;
    TQStringList list;

    line = line.simplifyWhiteSpace();
    while ((pos = line.find(delim)) != -1)
    {
        list.append(line.left(pos));
        line = line.mid(pos+1, line.length()-pos-1);
    }
    if (!line.isEmpty())
        list.append(line);
    return list;
}


const TQStringList fetchBranches(CvsService_stub* cvsService, TQWidget* parent)
{
    return FetchBranchesAndTags(TQString::fromLatin1("branch"), cvsService,
                                parent);
}


const TQStringList fetchTags(CvsService_stub* cvsService, TQWidget* parent)
{
    return FetchBranchesAndTags(TQString::fromLatin1("revision"), cvsService,
                                parent);
}


static TQStringList *tempFiles = 0;

void cleanupTempFiles()
{
    if (tempFiles)
    {
        TQStringList::Iterator it;
        for (it = tempFiles->begin(); it != tempFiles->end(); ++it)
            TQFile::remove(*it);
        delete tempFiles;
    }
}


TQString tempFileName(const TQString& suffix)
{
    if (!tempFiles)
        tempFiles = new TQStringList;

    KTempFile f(TQString(), suffix);
    tempFiles->append(f.name());
    return f.name();
}


int compareRevisions(const TQString& rev1, const TQString& rev2)
{
    const int length1(rev1.length());
    const int length2(rev2.length());

    // compare all parts of the revision

    int startPos1(0);
    int startPos2(0);
    while (startPos1 < length1 && startPos2 < length2)
    {
        int pos1(rev1.find('.', startPos1));
        if (pos1 < 0)
            pos1 = length1;
        const int partLength1(pos1 - startPos1);

        int pos2(rev2.find('.', startPos2));
        if (pos2 < 0)
            pos2 = length2;
        const int partLength2(pos2 - startPos2);

        // if the number of digits in both parts is not equal we are ready
        if (const int comp = ::compare(partLength1, partLength2))
            return comp;

        // if the parts are not equal we are ready
        if (const int comp = ::compare(rev1.mid(startPos1, partLength1),
                                       rev2.mid(startPos2, partLength2)))
            return comp;

        // continue with next part
        startPos1 = pos1 + 1;
        startPos2 = pos2 + 1;
    }

    // rev1 has more parts than rev2: rev2 < rev1
    if (startPos1 < length1)
        return 1;
    // rev2 has more parts than rev1: rev1 < rev2
    else if (startPos2 < length2)
        return -1;
    // all parts of rev1 and rev2 were compared (the number of parts is equal): rev1 == rev2
    else
        return 0;
}


// Local Variables:
// c-basic-offset: 4
// End: