summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sidebar/sq_storagefile.cpp
blob: 9fb921fd98ea744a0e7cfb2fd9c0e80b16f6fac2 (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
/***************************************************************************
                          sq_storagefile.cpp  -  description
                             -------------------
    begin                : Sat Mar 3 2007
    copyright            : (C) 2007 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qfile.h>
#include <qstring.h>
#include <qstring.h>

#include <kmdcodec.h>

#include "sq_storagefile.h"

void SQ_StorageFile::writeStorageFile(const QString &path, const KURL &inpath, KURL w)
{
    SQ_StorageFile::writeStorageFileAsString(path, inpath, w.prettyURL());
}

void SQ_StorageFile::writeStorageFileAsString(const QString &path, const KURL &inpath, QString content)
{
    if(content.isEmpty())
        content = inpath.prettyURL();

    KMD5 md5(QFile::encodeName(inpath.prettyURL()));
    QFile file(path + QString::fromLatin1(".") + QString(md5.hexDigest()));

    if(file.open(IO_WriteOnly))
    {
        QString k = content.utf8();
        file.writeBlock(k, k.length());
        file.close();
    }
}

KURL SQ_StorageFile::readStorageFile(const QString &path)
{
    QString n = SQ_StorageFile::readStorageFileAsString(path);

    int ind = n.find('\n');

    if(ind != -1)
        n.truncate(ind);

    return KURL::fromPathOrURL(n);
}

QString SQ_StorageFile::readStorageFileAsString(const QString &path)
{
    QFile file(path);
    QString str;

    if(file.open(IO_ReadOnly))
    {
        QByteArray ba = file.readAll();

        if(file.status() == IO_Ok)
        {
            QString k;
            str.append(ba);
            str = QString::fromUtf8(str);
        }
    }

    file.close();

    return str;
}