File: monitor_directory.cc

package info (click to toggle)
gtkmm-documentation 3.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 21,572 kB
  • sloc: cpp: 14,508; sh: 1,249; makefile: 1,042; xml: 104; perl: 64
file content (131 lines) | stat: -rw-r--r-- 3,825 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************
 *
 *  Copyright (c) 2007 Jonathon Jongsma
 *
 *  This file is part of gtkmm
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>
 *
 *******************************************************************************/
#include <iostream>
#include <giomm.h>

Glib::RefPtr<Glib::MainLoop> mainloop;
const std::string FILENAME = "./temp-file";

void
on_directory_changed(const Glib::RefPtr<Gio::File>& file,
           const Glib::RefPtr<Gio::File>& other_file,
           Gio::FileMonitorEvent event)
{
  std::cout << "** Directory changed **" << std::endl;

  if(file)
    std::cout << "   File 1: " << file->get_path () << std::endl;

  if(other_file)
    std::cout << "   File 2: " << other_file->get_path () << std::endl;

  std::cout << "   ";

  switch(event)
  {
    case Gio::FILE_MONITOR_EVENT_CHANGED:
      std::cout << "Event: A file is being changed" << std::endl;
      break;
    case Gio::FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
      std::cout << "Event: File changes are done" << std::endl;
      break;
    case Gio::FILE_MONITOR_EVENT_DELETED:
      std::cout << "Event: A file was deleted" << std::endl;
      break;
    case Gio::FILE_MONITOR_EVENT_CREATED:
      std::cout << "Event: A file was created" << std::endl;
      break;
    case Gio::FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
      std::cout << "Event: A file attribute was changed" << std::endl;
      break;
    case Gio::FILE_MONITOR_EVENT_PRE_UNMOUNT:
      std::cout << "Event: pre-unmount event" << std::endl;
      break;
    case Gio::FILE_MONITOR_EVENT_UNMOUNTED:
      std::cout << "Event: unmounted" << std::endl;
      break;
    default:
      g_assert_not_reached ();
  }
}

bool create_temp_file()
{
  try
  {
    auto temp_file = Gio::File::create_for_path(FILENAME);

    auto stream = temp_file->create_file ();
    stream->write("This is only a test.");
    stream->close();
  }
  catch (const Gio::Error& ex)
  {
    std::cout << ex.what () << std::endl;
  }

  // Stop future timeouts from repeating:
  return false;
}

bool quit ()
{
  try
  {
    auto temp_file = Gio::File::create_for_path(FILENAME);
    temp_file->trash();
  }
  catch (const Gio::Error& ex)
  {
    std::cout << ex.what () << std::endl;
  }

  // Stop future timeouts from repeating:
  mainloop->quit();

  return false;
}

int main(int /* argc */, char** /* argv */)
{
  Gio::init();
  mainloop = Glib::MainLoop::create();

  std::string current_dir = Glib::get_current_dir();
  auto dir = Gio::File::create_for_path(current_dir);
  auto monitor = dir->monitor_directory();

  std::cout << "Monitoring directory '" << current_dir << "'..."
    << std::endl << std::endl;
  monitor->signal_changed().connect(sigc::ptr_fun(on_directory_changed));

  std::cout << "Creating test file '" << FILENAME << "' to see what happens..."
    << std::endl << std::endl;

  // Wait a couple seconds and then create a temp file to trigger the
  // directory monitor.
  Glib::signal_timeout().connect_seconds(sigc::ptr_fun (&create_temp_file), 2);

  // Then exit a couple seconds later:
  Glib::signal_timeout().connect_seconds(sigc::ptr_fun (&quit), 4);
  mainloop->run();
  return 0;
}