| 1 | #include "common.hh" |
| 2 | #include "initpass.hh" |
| 3 | |
| 4 | #include <FL/Fl_Window.H> |
| 5 | #include <FL/fl_ask.H> |
| 6 | |
| 7 | #include <cerrno> |
| 8 | #include <fstream> |
| 9 | #include <iostream> |
| 10 | #include <string> |
| 11 | |
| 12 | #include <sys/stat.h> |
| 13 | |
| 14 | Initpass init; |
| 15 | |
| 16 | void Initpass::exec(const std::string &gpgid) { |
| 17 | std::string lang = Common::getlang(); |
| 18 | std::string basedir = Common::getbasedir(true); |
| 19 | |
| 20 | Common common; |
| 21 | if (common.mkdir_r(basedir, 0755) != 0 && errno != EEXIST) { |
| 22 | std::cout << basedir << std::endl; |
| 23 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 24 | "Failed to create directory." : |
| 25 | "ディレクトリを作成に失敗。"); |
| 26 | fl_alert("%s", err.c_str()); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | std::string gpgidpath = basedir + "/.gpg-id"; |
| 31 | |
| 32 | struct stat statbuf; |
| 33 | if (stat(gpgidpath.c_str(), &statbuf) == 0) { |
| 34 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 35 | ".gpg-id file already exists." : |
| 36 | ".gpg-idファイルは既に存在します。"); |
| 37 | fl_alert("%s", err.c_str()); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | std::ofstream gpgidfile(gpgidpath); |
| 42 | if (!gpgidfile.is_open()) { |
| 43 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 44 | "Failed to write .gpg-id file." : |
| 45 | ".gpg-idファイルを書き込めません。"); |
| 46 | fl_alert("%s", err.c_str()); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | gpgidfile << gpgid + '\n'; |
| 51 | |
| 52 | gpgidfile.close(); |
| 53 | |
| 54 | std::string msg = (lang.compare(0, 2, "en") == 0 ? |
| 55 | "Initialization completed." : |
| 56 | "初期設定に完了しました。"); |
| 57 | fl_alert("%s", msg.c_str()); |
| 58 | } |