PageRenderTime 29ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 3ms app.codeStats 0ms

/guitone-1.0rc5/src/monotone/MonotoneResourceFile.cpp

#
C++ | 77 lines | 50 code | 10 blank | 17 comment | 2 complexity | 27a6405d9e69c086cb70312b2bd10058 MD5 | raw file
Possible License(s): GPL-3.0
 1/***************************************************************************
 2 *   Copyright (C) 2010 by Thomas Keller                                   *
 3 *   me@thomaskeller.biz                                                   *
 4 *                                                                         *
 5 *   This program is free software; you can redistribute it and/or modify  *
 6 *   it under the terms of the GNU General Public License as published by  *
 7 *   the Free Software Foundation, either version 3 of the License, or     *
 8 *   (at your option) any later version.                                   *
 9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
18
19#include "MonotoneResourceFile.h"
20#include "Platform.h"
21#include "Settings.h"
22
23#include <QDir>
24#include <QDateTime>
25#include <QCryptographicHash>
26
27MonotoneResourceFile::MonotoneResourceFile() : QFile(0)
28{
29    QByteArray data;
30    data.append(QDateTime::currentDateTime().toString());
31    data.append(Platform::getUsername());
32    QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
33
34    QString tempPath = QDir::tempPath();
35    tempPath.append(QDir::separator()).append(hash.toHex()).append(".lua");
36    setFileName(tempPath);
37
38    open(QIODevice::ReadWrite);
39    setPermissions(QFile::ReadOwner | QFile::WriteOwner);
40
41    write(
42        "-- this is an automatically created configuration file\n"
43        "-- for a running monotone instance of guitone\n\n"
44    );
45
46    QMap<QString, QVariant> keyPasswords = Settings::getItemMap("KeyPasswords");
47    if (keyPasswords.size() > 0)
48    {
49        write("if type(get_passphrase) == 'function' then\n");
50        write("  old_get_passphrase = get_passphrase\n");
51        write("end\n\n");
52        write("function get_passphrase(key_identity)\n");
53        write("  if type(old_get_passphrase) == 'function' then\n");
54        write("    phrase = old_get_passphrase(key_identity)\n");
55        write("    if phrase ~= nil then\n");
56        write("      return phrase\n");
57        write("    end\n");
58        write("  end\n");
59
60        QMapIterator<QString, QVariant> it(keyPasswords);
61        while (it.hasNext())
62        {
63            it.next();
64            write("  if key_identity.id == \"" + it.key().toUtf8() + "\" then\n");
65            write("    return \"" + it.value().toString().toUtf8() + "\"\n");
66            write("  end\n");
67        }
68        write("end\n");
69    }
70    close();
71}
72
73MonotoneResourceFile::~MonotoneResourceFile()
74{
75    remove();
76}
77