| 1 | #include <FL/Fl.H> |
| 2 | #include <FL/Fl_PNG_Image.H> |
| 3 | #include <FL/Fl_JPEG_Image.H> |
| 4 | |
| 5 | #include <algorithm> |
| 6 | #include <cctype> |
| 7 | #include <filesystem> |
| 8 | #include <iostream> |
| 9 | #include <mutex> |
| 10 | |
| 11 | #include "thumbnail.hh" |
| 12 | #include "clickablebox.hh" |
| 13 | #include "misc.hh" |
| 14 | #include "widget.hh" |
| 15 | |
| 16 | namespace fe { |
| 17 | Thumbnail::Thumbnail() : loadingDialog(nullptr) {} |
| 18 | |
| 19 | Thumbnail::~Thumbnail() { |
| 20 | for (auto img : thumbs) { |
| 21 | delete img; |
| 22 | } |
| 23 | |
| 24 | if (loadingDialog) { |
| 25 | delete loadingDialog; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | void Thumbnail::createLoadingDialog() { |
| 30 | Fl_Window *mainWindow = Widget::getScroll()->window(); |
| 31 | if (!mainWindow) return; |
| 32 | |
| 33 | int w = 200; |
| 34 | int h = 100; |
| 35 | int sw = mainWindow->w(); |
| 36 | int sh = mainWindow->h(); |
| 37 | |
| 38 | int sx = mainWindow->x(); |
| 39 | int sy = mainWindow->y(); |
| 40 | int x = sx + (sw - w) / 2; |
| 41 | int y = sy + (sh - h) / 2; |
| 42 | |
| 43 | loadingDialog = new Fl_Window(x, y, w, h, "読み込み中"); |
| 44 | loadingDialog->set_modal(); |
| 45 | |
| 46 | Fl_Box *box = |
| 47 | new Fl_Box(0, 0, w, h, "画像を読み込んでいます。\nお待ち下さい。"); |
| 48 | box->align(FL_ALIGN_CENTER | FL_ALIGN_INSIDE); |
| 49 | |
| 50 | loadingDialog->end(); |
| 51 | loadingDialog->show(); |
| 52 | loadingDialog->take_focus(); |
| 53 | } |
| 54 | |
| 55 | void Thumbnail::init() { |
| 56 | const char *home = getenv("HOME"); |
| 57 | std::string homedir = ""; |
| 58 | if (home) homedir = std::string(home) + "/.local/share/wallpapers"; |
| 59 | std::string localdir = "/usr/local/share/wallpapers"; |
| 60 | std::string sysdir = "/usr/share/wallpapers"; |
| 61 | |
| 62 | if (Misc::isExist(homedir)) { |
| 63 | std::vector<std::string> img = listImages(homedir); |
| 64 | imgfiles.insert(imgfiles.end(), img.begin(), img.end()); |
| 65 | } |
| 66 | if (Misc::isExist(localdir)) { |
| 67 | std::vector<std::string> img = listImages(localdir); |
| 68 | imgfiles.insert(imgfiles.end(), img.begin(), img.end()); |
| 69 | } |
| 70 | if (Misc::isExist(sysdir)) { |
| 71 | std::vector<std::string> img = listImages(sysdir); |
| 72 | imgfiles.insert(imgfiles.end(), img.begin(), img.end()); |
| 73 | } |
| 74 | |
| 75 | Widget::getScroll()->deactivate(); |
| 76 | |
| 77 | for (const auto &file : imgfiles) { |
| 78 | pending.push(file); |
| 79 | } |
| 80 | |
| 81 | createLoadingDialog(); |
| 82 | Fl::add_timeout(0.0, loadImagesCb, this); |
| 83 | } |
| 84 | |
| 85 | void Thumbnail::loadImagesCb(void *v) { |
| 86 | Thumbnail *thumb = static_cast<Thumbnail *>(v); |
| 87 | thumb->loadNextImage(); |
| 88 | } |
| 89 | |
| 90 | void Thumbnail::loadNextImage() { |
| 91 | std::lock_guard<std::mutex> lock(mutex); |
| 92 | |
| 93 | if (pending.empty()) { |
| 94 | if (loadingDialog) { |
| 95 | loadingDialog->hide(); |
| 96 | delete loadingDialog; |
| 97 | loadingDialog = nullptr; |
| 98 | } |
| 99 | Widget::getScroll()->activate(); |
| 100 | Widget::getScroll()->redraw(); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | std::string filepath = pending.front(); |
| 105 | pending.pop(); |
| 106 | |
| 107 | Fl_Image *img = nullptr; |
| 108 | std::string ext = filepath.substr(filepath.size() - 4); |
| 109 | |
| 110 | if (ext == ".png") { |
| 111 | img = new Fl_PNG_Image(filepath.c_str()); |
| 112 | } else if (ext == ".jpg") { |
| 113 | img = new Fl_JPEG_Image(filepath.c_str()); |
| 114 | } |
| 115 | |
| 116 | if (!img || img->w() <= 0 || img->h() <= 0) { |
| 117 | if (img) delete img; |
| 118 | if (!pending.empty()) Fl::add_timeout(0.0, loadImagesCb, this); |
| 119 | else { |
| 120 | if (loadingDialog) { |
| 121 | loadingDialog->hide(); |
| 122 | delete loadingDialog; |
| 123 | loadingDialog = nullptr; |
| 124 | } |
| 125 | Widget::getScroll()->activate(); |
| 126 | Widget::getScroll()->redraw(); |
| 127 | } |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | Fl_Image *thumb = img->copy(100, 100); |
| 132 | thumbs.push_back(thumb); |
| 133 | delete img; |
| 134 | |
| 135 | ClickableBox *box = new ClickableBox(curX, curY, 100, 100); |
| 136 | box->image(thumb); |
| 137 | box->box(FL_FLAT_BOX); |
| 138 | box->setImagePath(filepath.c_str()); |
| 139 | |
| 140 | Widget::getScroll()->add(box); |
| 141 | Widget::getScroll()->redraw(); |
| 142 | |
| 143 | curX += 110; |
| 144 | if (curX + 110 > Widget::getScroll()->w()) { |
| 145 | curX = 10; |
| 146 | curY += 110; |
| 147 | } |
| 148 | |
| 149 | if (!pending.empty()) Fl::add_timeout(0.01, loadImagesCb, this); |
| 150 | else { |
| 151 | if (loadingDialog) { |
| 152 | loadingDialog->hide(); |
| 153 | delete loadingDialog; |
| 154 | loadingDialog = nullptr; |
| 155 | } |
| 156 | Widget::getScroll()->activate(); |
| 157 | Widget::getScroll()->redraw(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | std::vector<std::string> Thumbnail::listImages(const std::string &directory) { |
| 162 | std::vector<std::string> images; |
| 163 | std::filesystem::path dir(directory); |
| 164 | |
| 165 | if (!std::filesystem::exists(dir) || !std::filesystem::is_directory(dir)) { |
| 166 | std::cerr << "ディレクトリを開くに失敗: " << directory << std::endl; |
| 167 | return images; |
| 168 | } |
| 169 | |
| 170 | for (const auto &entry : std::filesystem::directory_iterator(dir)) { |
| 171 | if (!entry.is_regular_file()) continue; |
| 172 | |
| 173 | std::string filename = entry.path().filename().string(); |
| 174 | if (filename.size() <= 4) continue; |
| 175 | |
| 176 | std::string ext = entry.path().extension().string(); |
| 177 | std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower); |
| 178 | |
| 179 | if (ext == ".png" || ext == ".jpg") { |
| 180 | images.push_back(entry.path().string()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return images; |
| 185 | } |
| 186 | } |