Debian Codegraph @

CodeLite/ssh_account_info.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : ssh_account_info.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "ssh_account_info.h"
27
28 #include "xor_string.h"
29
30 SSHAccountInfo::SSHAccountInfo()
31 : clConfigItem("ssh-account")
32 , m_port(22)
33 {
34 }
35
36 SSHAccountInfo::~SSHAccountInfo() {}
37
38 SSHAccountInfo& SSHAccountInfo::operator=(const SSHAccountInfo& other)
39 {
40 if(&other == this) {
41 return *this;
42 }
43 m_accountName = other.m_accountName;
44 m_username = other.m_username;
45 m_password = other.m_password;
46 m_port = other.m_port;
47 m_host = other.m_host;
48 m_bookmarks = other.m_bookmarks;
49 m_defaultFolder = other.m_defaultFolder;
50 return *this;
51 }
52
53 void SSHAccountInfo::FromJSON(const JSONItem& json)
54 {
55 m_accountName = json.namedObject("m_accountName").toString();
56 m_username = json.namedObject("m_username").toString();
57 wxString pass = json.namedObject("m_password").toString();
58 XORString x(pass);
59 m_password = x.Decrypt();
60 m_port = json.namedObject("m_port").toInt(22);
61 m_host = json.namedObject("m_host").toString();
62 m_bookmarks = json.namedObject("m_bookmarks").toArrayString();
63 m_defaultFolder = json.namedObject("m_defaultFolder").toString();
64 }
65
66 JSONItem SSHAccountInfo::ToJSON() const
67 {
68 JSONItem element = JSONItem::createObject(GetName());
69 element.addProperty("m_accountName", m_accountName);
70 element.addProperty("m_username", m_username);
71 element.addProperty("m_port", m_port);
72 element.addProperty("m_host", m_host);
73 element.addProperty("m_bookmarks", m_bookmarks);
74 element.addProperty("m_defaultFolder", m_defaultFolder);
75 XORString x(m_password);
76 element.addProperty("m_password", x.Encrypt());
77 return element;
78 }
79
80 void SSHAccountInfo::AddBookmark(const wxString& location)
81 {
82 if(m_bookmarks.Index(location) == wxNOT_FOUND) {
83 m_bookmarks.Add(location);
84 }
85 }
86
87 SSHAccountInfo::Vect_t SSHAccountInfo::Load(const std::function<bool(const SSHAccountInfo&)>& matcher)
88 {
89 wxFileName jsonfile(clStandardPaths::Get().GetUserDataDir(), "sftp-settings.conf");
90 jsonfile.AppendDir("config");
91
92 JSON root(jsonfile);
93 if(!root.isOk()) {
94 return {};
95 }
96 JSONItem s = root.toElement()["sftp-settings"];
97 auto accounts = s["accounts"];
98 size_t count = accounts.arraySize();
99 if(count == 0) {
100 return {};
101 }
102 Vect_t accountsVec;
103 accountsVec.reserve(count);
104 for(size_t i = 0; i < count; ++i) {
105 auto account = accounts[i];
106 SSHAccountInfo ai;
107 ai.FromJSON(account);
108 if(!matcher || (matcher && matcher(ai))) {
109 accountsVec.emplace_back(ai);
110 }
111 }
112 return accountsVec;
113 }
114
115 SSHAccountInfo SSHAccountInfo::LoadAccount(const wxString& accountName)
116 {
117 auto res = Load([&](const SSHAccountInfo& ai) { return ai.GetAccountName() == accountName; });
118 if(res.size() == 1) {
119 return res[0];
120 }
121 return {};
122 }
123
124 wxString SSHAccountInfo::GetSSHClient()
125 {
126 wxFileName jsonfile(clStandardPaths::Get().GetUserDataDir(), "sftp-settings.conf");
127 jsonfile.AppendDir("config");
128
129 JSON root(jsonfile);
130 if(!root.isOk()) {
131 return wxEmptyString;
132 }
133 return root.toElement()["sftp-settings"]["sshClient"].toString();
134 }