File: Monitor.pm

package info (click to toggle)
system-tools-backends 2.10.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,412 kB
  • sloc: perl: 15,454; sh: 10,177; ansic: 824; makefile: 202
file content (101 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (7)
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
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

# Authors: Carlos Garnacho Parro  <carlosg@gnome.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

package Utils::Monitor;

use Cwd;
use strict;
use base qw(Net::DBus::Object);
use Utils::Backend;
eval "use Sys::Gamin;";
my $eval_gamin = $@;

my $has_gamin = ($eval_gamin eq "") ? 1 : 0;
my $fm;
my %objects;

if ($has_gamin)
{
  $fm = new Sys::Gamin;
}

sub do_monitor_files
{
  my ($event, $data, $func, $path, $object);

  return if (!$has_gamin);

  while ($fm->pending)
  {
    $event = $fm->next_event;
    
    if ($event->type eq "change" ||
        $event->type eq "create")
    {
      $data = $objects {$event->filename};
      $object = $$data{"object"};

      $object->emit_signal ($$data{"signal"});
    }
  }
}

sub add_file
{
  my ($file, $object, $name, $signal) = @_;
  my ($path);
  
  $path = &Cwd::abs_path ($file);
  return unless -f $path;

  $objects {$path} = { "object" => $object,
                       "name"   => $name,
                       "signal" => $signal};
  $fm->monitor ($path);
}

sub monitor_files
{
  my ($files, $object, $name, $signal) = @_;
  my ($f);

  return if (!$has_gamin);

  if (ref $files eq "ARRAY")
  {
    foreach $f (@$files)
    {
      &add_file ($f, $object, $name, $signal);
    }
  }
  else
  {
    &add_file ($files, $object, $name, $signal);
  }
}

sub init_file_monitor
{
  return if (!$has_gamin);

  # should not use internal stuff in $fm like that
  Net::DBus::Reactor->main->add_read ($fm->{conn}->fd (),
                                      Net::DBus::Callback->new(method => \&Utils::Monitor::do_monitor_files));
}

1;