1 /*
2  * Copyright (c) 2002-2007  Daniel Elstner  <daniel.kitta@gmail.com>
3  *
4  * This file is part of regexxer.
5  *
6  * regexxer is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * regexxer is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with regexxer; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #include "signalutils.h"
22 
23 
24 namespace Util
25 {
26 
27 /**** Util::QueuedSignal ***************************************************/
28 
QueuedSignal(int priority)29 QueuedSignal::QueuedSignal(int priority)
30 :
31   signal_   (),
32   priority_ (priority),
33   queued_   (false)
34 {}
35 
~QueuedSignal()36 QueuedSignal::~QueuedSignal()
37 {}
38 
connect(const sigc::slot<void> & slot)39 sigc::connection QueuedSignal::connect(const sigc::slot<void>& slot)
40 {
41   return signal_.connect(slot);
42 }
43 
queue()44 void QueuedSignal::queue()
45 {
46   if (!queued_)
47   {
48     Glib::signal_idle().connect(sigc::mem_fun(*this, &QueuedSignal::idle_handler), priority_);
49     queued_ = true;
50   }
51 }
52 
idle_handler()53 bool QueuedSignal::idle_handler()
54 {
55   queued_ = false;
56   signal_(); // emit
57 
58   return false; // disconnect idle handler
59 }
60 
61 /**** Util::AutoConnection *************************************************/
62 
AutoConnection()63 AutoConnection::AutoConnection()
64 :
65   connection_ (),
66   blocked_    (false)
67 {}
68 
AutoConnection(const sigc::connection & connection)69 AutoConnection::AutoConnection(const sigc::connection& connection)
70 :
71   connection_ (connection),
72   blocked_    (connection_.blocked())
73 {}
74 
~AutoConnection()75 AutoConnection::~AutoConnection()
76 {
77   connection_.disconnect();
78 }
79 
block()80 void AutoConnection::block()
81 {
82   connection_.block();
83   blocked_ = true;
84 }
85 
unblock()86 void AutoConnection::unblock()
87 {
88   connection_.unblock();
89   blocked_ = false;
90 }
91 
operator =(const sigc::connection & connection)92 AutoConnection& AutoConnection::operator=(const sigc::connection& connection)
93 {
94   AutoConnection temp (connection_);
95 
96   connection_ = connection;
97   connection_.block(blocked_);
98 
99   return *this;
100 }
101 
disconnect()102 void AutoConnection::disconnect()
103 {
104   connection_.disconnect();
105 }
106 
107 } // namespace Util
108