| 1 | #include "common.hh" |
| 2 | #include "editpass.hh" |
| 3 | #include "delpass.hh" |
| 4 | #include "addpass.hh" |
| 5 | #include "../main.hh" |
| 6 | |
| 7 | #include <FL/Fl_Window.H> |
| 8 | #include <FL/fl_ask.H> |
| 9 | |
| 10 | #include <string> |
| 11 | #include <unistd.h> |
| 12 | #include <vector> |
| 13 | |
| 14 | Editpass edit; |
| 15 | |
| 16 | struct InputData { |
| 17 | Fl_Input *txtin; |
| 18 | Fl_Secret_Input *pass1; |
| 19 | Fl_Secret_Input *pass2; |
| 20 | Fl_Window *dialog; |
| 21 | }; |
| 22 | |
| 23 | void Editpass::setFile(std::string &f) { |
| 24 | edit.file = f; |
| 25 | } |
| 26 | |
| 27 | std::string Editpass::getFile() { |
| 28 | return file; |
| 29 | } |
| 30 | |
| 31 | bool Editpass::exec(const std::string &file, const std::string &pass) { |
| 32 | std::string lang = Common::getlang(); |
| 33 | |
| 34 | Common c; |
| 35 | c.tmpcopy(file, "/tmp/simpas-tmp.gpg"); |
| 36 | |
| 37 | Delpass d; |
| 38 | bool isdel = d.exec(file, true); |
| 39 | if (!isdel) { |
| 40 | std::string err = |
| 41 | (lang.compare(0, 2, "en") == 0 ? "Failed to edit." : "編集に失敗。"); |
| 42 | fl_alert("%s", err.c_str()); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | Addpass a; |
| 47 | bool isadd = a.exec(file, pass, true); |
| 48 | if (!isadd) { |
| 49 | std::string err = |
| 50 | (lang.compare(0, 2, "en") == 0 ? "Failed to edit." : "編集に失敗。"); |
| 51 | fl_alert("%s", err.c_str()); |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | c.tmpcopy("/tmp/simpas-tmp.gpg", file); |
| 68 | unlink("/tmp/simpas-tmp.gpg"); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | std::string msg = |
| 73 | (lang.compare(0, 2, "en") == 0 ? "Edit success." : "編集に成功。"); |
| 74 | fl_alert("%s", msg.c_str()); |
| 75 | unlink("/tmp/simpas-tmp.gpg"); |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void Editpass::edit_cb(Fl_Widget *, void *data) { |
| 81 | InputData *inputs = (InputData *)data; |
| 82 | std::string lang = Common::getlang(); |
| 83 | |
| 84 | if (inputs) { |
| 85 | file = inputs->txtin->value(); |
| 86 | if (file.empty()) { |
| 87 | std::string err = |
| 88 | (lang.compare(0, 2, "en") == 0 ? |
| 89 | "Please fill in the path." : |
| 90 | "パスをご入力下さい。"); |
| 91 | fl_alert("%s", err.c_str()); |
| 92 | return; |
| 93 | } |
| 94 | inputpass1 = inputs->pass1->value(); |
| 95 | if (inputpass1.empty()) { |
| 96 | std::string err = |
| 97 | (lang.compare(0, 2, "en") == 0 ? |
| 98 | "Please fill in the password." : |
| 99 | "パスワードをご入力下さい。"); |
| 100 | fl_alert("%s", err.c_str()); |
| 101 | return; |
| 102 | } |
| 103 | inputpass2 = inputs->pass2->value(); |
| 104 | if (inputpass2.empty()) { |
| 105 | std::string err = |
| 106 | (lang.compare(0, 2, "en") == 0 ? |
| 107 | "Please fill in the password (confirm)." : |
| 108 | "パスワード (確認)をご入力下さい。"); |
| 109 | fl_alert("%s", err.c_str()); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | if (inputpass1 != inputpass2) { |
| 114 | std::string err = |
| 115 | (lang.compare(0, 2, "en") == 0 ? |
| 116 | "Password does not match." : |
| 117 | "パスワードが一致していません。"); |
| 118 | fl_alert("%s", err.c_str()); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (exec(file, inputpass1)) { |
| 123 | inputs->dialog->hide(); |
| 124 | } |
| 125 | } else { |
| 126 | std::string err = |
| 127 | (lang.compare(0, 2, "en") == 0 ? |
| 128 | "Please fill in all the fields." : |
| 129 | "全てのフィールドをご入力下さい。"); |
| 130 | fl_alert("%s", err.c_str()); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void Editpass::dialog_cb(Fl_Widget *w, void *data) { |
| 135 | (void)w; |
| 136 | (void)data; |
| 137 | std::string lang = Common::getlang(); |
| 138 | Fl_Window *dialog = new Fl_Window(400, 160, |
| 139 | (lang.compare(0, 2, "en") == 0 ? "Edit password" : "パスワードの編集")); |
| 140 | |
| 141 | Fl_Input *txtin = new Fl_Input(150, 20, 180, 30, |
| 142 | (lang.compare(0, 2, "en") == 0 ? "Path:" : "パス:")); |
| 143 | Fl_Secret_Input *pass1 = new Fl_Secret_Input(150, 20, 180, 30, |
| 144 | (lang.compare(0, 2, "en") == 0 ? "Password:" : "パスワード:")); |
| 145 | Fl_Secret_Input *pass2 = new Fl_Secret_Input(150, 60, 180, 30, |
| 146 | (lang.compare(0, 2, "en") == 0 ? "Password (confirm):" : "パスワード (確認):")); |
| 147 | dialog->add(pass1); |
| 148 | dialog->add(pass2); |
| 149 | txtin->hide(); |
| 150 | txtin->value(edit.getFile().c_str()); |
| 151 | |
| 152 | InputData *inputs = new InputData(); |
| 153 | inputs->txtin = txtin; |
| 154 | inputs->pass1 = pass1; |
| 155 | inputs->pass2 = pass2; |
| 156 | inputs->dialog = dialog; |
| 157 | |
| 158 | Fl_Button *okbtn = new Fl_Button(60, 110, 80, 30, "OK"); |
| 159 | Fl_Button *cancelbtn = new Fl_Button(160, 110, 80, 30, |
| 160 | (lang.compare(0, 2, "en") == 0 ? "Cancel" : "キャンセル")); |
| 161 | |
| 162 | okbtn->callback(static_ok_cb, inputs); |
| 163 | cancelbtn->callback(static_cancel_cb, dialog); |
| 164 | |
| 165 | dialog->add(okbtn); |
| 166 | dialog->add(cancelbtn); |
| 167 | |
| 168 | dialog->end(); |
| 169 | dialog->set_modal(); |
| 170 | dialog->show(); |
| 171 | } |
| 172 | |
| 173 | void Editpass::static_ok_cb(Fl_Widget *w, void *data) { |
| 174 | (void)w; |
| 175 | InputData *inputs = (InputData *)data; |
| 176 | |
| 177 | edit.edit_cb(nullptr, inputs); |
| 178 | std::vector<std::string> fpaths; |
| 179 | std::string rdir = Common::getbasedir(false); |
| 180 | std::string curpath = inputs->txtin->value(); |
| 181 | clearpaths(false, curpath); |
| 182 | scandir(rdir, rdir, fpaths); |
| 183 | updatelist(); |
| 184 | browse(curpath, false); |
| 185 | } |
| 186 | |
| 187 | void Editpass::static_cancel_cb(Fl_Widget *w, void *data) { |
| 188 | ((Editpass *)data)->cancel_cb(w, data); |
| 189 | } |