| 1 | #include "common.hh" |
| 2 | #include "delpass.hh" |
| 3 | #include "../main.hh" |
| 4 | |
| 5 | #include <FL/fl_ask.H> |
| 6 | #include <FL/Fl_Input.H> |
| 7 | |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | Delpass del; |
| 11 | |
| 12 | struct InputData { |
| 13 | Fl_Input *txtin; |
| 14 | Fl_Window *dialog; |
| 15 | }; |
| 16 | |
| 17 | void Delpass::setFile(std::string &f) { |
| 18 | del.file = f; |
| 19 | } |
| 20 | |
| 21 | std::string Delpass::getFile() { |
| 22 | return file; |
| 23 | } |
| 24 | |
| 25 | bool Delpass::exec(const std::string &file, bool force) { |
| 26 | std::string lang = Common::getlang(); |
| 27 | |
| 28 | std::string basedir = Common::getbasedir(true); |
| 29 | std::string ext = ".gpg"; |
| 30 | |
| 31 | |
| 32 | if (access(file.c_str(), F_OK) != 0) { |
| 33 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 34 | "Password does not exist" : |
| 35 | "パスワードが存在しません"); |
| 36 | fl_alert("%s", err.c_str()); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if (unlink(file.c_str()) == -1) { |
| 41 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 42 | "Password cannot be deleted" : |
| 43 | "パスワードを削除出来ませんですた"); |
| 44 | fl_alert("%s", err.c_str()); |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | std::vector<std::string> tokens = Common::explode(file, '/'); |
| 50 | std::string passpath = basedir + tokens[0]; |
| 51 | |
| 52 | for (size_t i = 1; i < tokens.size(); ++i) { |
| 53 | if (i == tokens.size() - 1) continue; |
| 54 | passpath += "/" + tokens[i]; |
| 55 | } |
| 56 | |
| 57 | for (int i = tokens.size() - 1; i >= 0; --i) { |
| 58 | |
| 59 | if (passpath.compare(0, basedir.size(), basedir) == 0) { |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | if (rmdir(passpath.c_str()) == -1) { |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | size_t last_slash = passpath.find_last_of('/'); |
| 69 | if (last_slash != std::string::npos) { |
| 70 | passpath.erase(last_slash); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (force) return true; |
| 75 | |
| 76 | std::string msg = (lang.compare(0, 2, "en") == 0 ? |
| 77 | "The password got deleted" : |
| 78 | "パスワードを削除しました"); |
| 79 | fl_alert("%s", msg.c_str()); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void Delpass::delete_cb(Fl_Widget *, void *data) { |
| 84 | InputData *inputs = (InputData *)data; |
| 85 | |
| 86 | if (inputs) { |
| 87 | file = inputs->txtin->value(); |
| 88 | } |
| 89 | |
| 90 | exec(file, false); |
| 91 | std::vector<std::string> fpaths; |
| 92 | std::string rdir = Common::getbasedir(false); |
| 93 | |
| 94 | std::string mockpath = ""; |
| 95 | clearpaths(true, mockpath); |
| 96 | scandir(rdir, rdir, fpaths); |
| 97 | updatelist(); |
| 98 | } |
| 99 | |
| 100 | void Delpass::dialog_cb(Fl_Widget *w, void *data) { |
| 101 | (void)w; |
| 102 | (void)data; |
| 103 | std::string lang = Common::getlang(); |
| 104 | |
| 105 | Fl_Input *txtin = new Fl_Input(150, 20, 180, 30, |
| 106 | (lang.compare(0, 2, "en") == 0 ? "Path:" : "パス:")); |
| 107 | txtin->hide(); |
| 108 | txtin->value(del.getFile().c_str()); |
| 109 | |
| 110 | InputData *inputs = new InputData(); |
| 111 | inputs->txtin = txtin; |
| 112 | |
| 113 | std::string asking = |
| 114 | (lang.compare(0, 2, "en") == 0 ? |
| 115 | "Are you sure you want to delete the password '" + del.getFile() + "'?" : |
| 116 | "パスワード「" + del.getFile() + "」を本当に削除する事が宜しいでしょうか?"); |
| 117 | int confirm = fl_choice("%s", |
| 118 | (lang.compare(0, 2, "en") == 0 ? "Cancel" : "キャンセル"), |
| 119 | (lang.compare(0, 2, "en") == 0 ? "Delete" : "削除"), |
| 120 | nullptr, asking.c_str()); |
| 121 | |
| 122 | if (confirm == 0) { |
| 123 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 124 | "Not deleted" : |
| 125 | "削除しませんでした"); |
| 126 | fl_alert("%s", err.c_str()); |
| 127 | } else { |
| 128 | static_ok_cb(w, inputs); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void Delpass::static_ok_cb(Fl_Widget *w, void *data) { |
| 133 | (void)w; |
| 134 | InputData *inputs = (InputData *)data; |
| 135 | |
| 136 | del.delete_cb(nullptr, inputs); |
| 137 | std::vector<std::string> fpaths; |
| 138 | std::string rdir = Common::getbasedir(false); |
| 139 | std::string curpath = ""; |
| 140 | clearpaths(true, curpath); |
| 141 | scandir(rdir, rdir, fpaths); |
| 142 | updatelist(); |
| 143 | } |
| 144 | |
| 145 | void Delpass::static_cancel_cb(Fl_Widget *w, void *data) { |
| 146 | ((Delpass *)data)->cancel_cb(w, data); |
| 147 | } |