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

# · C++ · 77 lines · 50 code · 10 blank · 17 comment · 2 complexity · 27a6405d9e69c086cb70312b2bd10058 MD5 · raw file

  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. #include "MonotoneResourceFile.h"
  19. #include "Platform.h"
  20. #include "Settings.h"
  21. #include <QDir>
  22. #include <QDateTime>
  23. #include <QCryptographicHash>
  24. MonotoneResourceFile::MonotoneResourceFile() : QFile(0)
  25. {
  26. QByteArray data;
  27. data.append(QDateTime::currentDateTime().toString());
  28. data.append(Platform::getUsername());
  29. QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
  30. QString tempPath = QDir::tempPath();
  31. tempPath.append(QDir::separator()).append(hash.toHex()).append(".lua");
  32. setFileName(tempPath);
  33. open(QIODevice::ReadWrite);
  34. setPermissions(QFile::ReadOwner | QFile::WriteOwner);
  35. write(
  36. "-- this is an automatically created configuration file\n"
  37. "-- for a running monotone instance of guitone\n\n"
  38. );
  39. QMap<QString, QVariant> keyPasswords = Settings::getItemMap("KeyPasswords");
  40. if (keyPasswords.size() > 0)
  41. {
  42. write("if type(get_passphrase) == 'function' then\n");
  43. write(" old_get_passphrase = get_passphrase\n");
  44. write("end\n\n");
  45. write("function get_passphrase(key_identity)\n");
  46. write(" if type(old_get_passphrase) == 'function' then\n");
  47. write(" phrase = old_get_passphrase(key_identity)\n");
  48. write(" if phrase ~= nil then\n");
  49. write(" return phrase\n");
  50. write(" end\n");
  51. write(" end\n");
  52. QMapIterator<QString, QVariant> it(keyPasswords);
  53. while (it.hasNext())
  54. {
  55. it.next();
  56. write(" if key_identity.id == \"" + it.key().toUtf8() + "\" then\n");
  57. write(" return \"" + it.value().toString().toUtf8() + "\"\n");
  58. write(" end\n");
  59. }
  60. write("end\n");
  61. }
  62. close();
  63. }
  64. MonotoneResourceFile::~MonotoneResourceFile()
  65. {
  66. remove();
  67. }