| 1 |
/******************************************************************************* |
| 2 |
* |
| 3 |
* Copyright (c) 2007 Jonathon Jongsma |
| 4 |
* |
| 5 |
* This file is part of gtkmm |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, see <http://www.gnu.org/licenses/> |
| 19 |
* |
| 20 |
*******************************************************************************/ |
| 21 |
#include <iostream> |
| 22 |
#include <utility> |
| 23 |
#include <giomm.h> |
| 24 |
|
| 25 |
// How to connect on_directory_changed() to a signal: |
| 26 |
// 1. sigc::ptr_fun() |
| 27 |
// 2. C++11 lambda expression with explicit parameters |
| 28 |
// 3. C++14 variadic lambda expression with perfect forwarding |
| 29 |
#define LAMBDA_EXPRESSION_OR_PTR_FUN 1 |
| 30 |
|
| 31 |
namespace |
| 32 |
{ |
| 33 |
|
| 34 |
Glib::RefPtr<Glib::MainLoop> mainloop; |
| 35 |
const std::string FILENAME = "./temp-file"; |
| 36 |
|
| 37 |
void |
| 38 |
on_directory_changed(const Glib::RefPtr<Gio::File>& file, |
| 39 |
const Glib::RefPtr<Gio::File>& other_file, |
| 40 |
Gio::FileMonitor::Event event) |
| 41 |
{ |
| 42 |
std::cout << "** Directory changed **" << std::endl; |
| 43 |
|
| 44 |
if(file) |
| 45 |
std::cout << " File 1: " << file->get_path() << std::endl; |
| 46 |
|
| 47 |
if(other_file) |
| 48 |
std::cout << " File 2: " << other_file->get_path() << std::endl; |
| 49 |
|
| 50 |
std::cout << " Event: "; |
| 51 |
|
| 52 |
switch(event) |
| 53 |
{ |
| 54 |
case Gio::FileMonitor::Event::CHANGED: |
| 55 |
std::cout << "A file changed" << std::endl; |
| 56 |
break; |
| 57 |
case Gio::FileMonitor::Event::CHANGES_DONE_HINT: |
| 58 |
std::cout << "A hint that this was probably the last change in a set of changes" << std::endl; |
| 59 |
break; |
| 60 |
case Gio::FileMonitor::Event::DELETED: |
| 61 |
std::cout << "A file was deleted" << std::endl; |
| 62 |
break; |
| 63 |
case Gio::FileMonitor::Event::CREATED: |
| 64 |
std::cout << "A file was created" << std::endl; |
| 65 |
break; |
| 66 |
case Gio::FileMonitor::Event::ATTRIBUTE_CHANGED: |
| 67 |
std::cout << "A file attribute was changed" << std::endl; |
| 68 |
break; |
| 69 |
case Gio::FileMonitor::Event::PRE_UNMOUNT: |
| 70 |
std::cout << "The file location will soon be unmounted" << std::endl; |
| 71 |
break; |
| 72 |
case Gio::FileMonitor::Event::UNMOUNTED: |
| 73 |
std::cout << "The file location was unmounted" << std::endl; |
| 74 |
break; |
| 75 |
case Gio::FileMonitor::Event::MOVED: |
| 76 |
std::cout << "The file was moved" << std::endl; |
| 77 |
break; |
| 78 |
case Gio::FileMonitor::Event::RENAMED: |
| 79 |
std::cout << "The file was renamed within the current directory" << std::endl; |
| 80 |
break; |
| 81 |
case Gio::FileMonitor::Event::MOVED_IN: |
| 82 |
std::cout << "The file was moved into the monitored directory from another location" << std::endl; |
| 83 |
break; |
| 84 |
case Gio::FileMonitor::Event::MOVED_OUT: |
| 85 |
std::cout << "The file was moved out of the monitored directory to another location" << std::endl; |
| 86 |
break; |
| 87 |
default: |
| 88 |
std::cout << "Unknown event " << static_cast<int>(event) << std::endl; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
bool create_temp_file() |
| 93 |
{ |
| 94 |
try |
| 95 |
{ |
| 96 |
auto temp_file = Gio::File::create_for_path(FILENAME); |
| 97 |
|
| 98 |
auto stream = temp_file->create_file (); |
| 99 |
stream->write("This is only a test."); |
| 100 |
stream->close(); |
| 101 |
} |
| 102 |
catch (const Gio::Error& ex) |
| 103 |
{ |
| 104 |
std::cout << ex.what () << std::endl; |
| 105 |
} |
| 106 |
|
| 107 |
// Stop future timeouts from repeating: |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
bool quit() |
| 112 |
{ |
| 113 |
try |
| 114 |
{ |
| 115 |
auto temp_file = Gio::File::create_for_path(FILENAME); |
| 116 |
temp_file->remove(); |
| 117 |
} |
| 118 |
catch (const Gio::Error& ex) |
| 119 |
{ |
| 120 |
std::cout << ex.what () << std::endl; |
| 121 |
} |
| 122 |
|
| 123 |
// Stop future timeouts from repeating: |
| 124 |
mainloop->quit(); |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
} // anomymous namespace |
| 130 |
|
| 131 |
int main(int /* argc */, char** /* argv */) |
| 132 |
{ |
| 133 |
Gio::init(); |
| 134 |
mainloop = Glib::MainLoop::create(); |
| 135 |
|
| 136 |
auto current_dir = Glib::get_current_dir(); |
| 137 |
auto dir = Gio::File::create_for_path(current_dir); |
| 138 |
auto monitor = dir->monitor_directory(Gio::FileMonitorFlags::WATCH_MOVES); |
| 139 |
|
| 140 |
std::cout << "Monitoring directory '" << current_dir << "'..." |
| 141 |
<< std::endl << std::endl; |
| 142 |
#if LAMBDA_EXPRESSION_OR_PTR_FUN == 1 |
| 143 |
// sigc::ptr_fun() can sometimes be nicer than a lambda expression. |
| 144 |
monitor->signal_changed().connect(sigc::ptr_fun(on_directory_changed)); |
| 145 |
#elif LAMBDA_EXPRESSION_OR_PTR_FUN == 2 |
| 146 |
monitor->signal_changed().connect( |
| 147 |
[](const Glib::RefPtr<Gio::File>& file, |
| 148 |
const Glib::RefPtr<Gio::File>& other_file, |
| 149 |
Gio::FileMonitor::Event event) |
| 150 |
{ on_directory_changed(file, other_file, event); } |
| 151 |
); |
| 152 |
#elif LAMBDA_EXPRESSION_OR_PTR_FUN == 3 |
| 153 |
monitor->signal_changed().connect( |
| 154 |
[](auto&&... pars) |
| 155 |
{ on_directory_changed(std::forward<decltype(pars)>(pars)...); } |
| 156 |
); |
| 157 |
#else |
| 158 |
#error Unknown value of LAMBDA_EXPRESSION_OR_PTR_FUN |
| 159 |
#endif |
| 160 |
std::cout << "Creating test file '" << FILENAME << "' to see what happens..." |
| 161 |
<< std::endl << std::endl; |
| 162 |
|
| 163 |
// Wait a couple seconds and then create a temp file to trigger the |
| 164 |
// directory monitor. |
| 165 |
Glib::signal_timeout().connect_seconds([](){ return create_temp_file(); }, 2); |
| 166 |
|
| 167 |
// Then exit when the user has had some time to make changes in |
| 168 |
// the monitored directory. |
| 169 |
Glib::signal_timeout().connect_seconds([](){ return quit(); }, 30); |
| 170 |
mainloop->run(); |
| 171 |
return 0; |
| 172 |
} |