/[dino]/glashctl/lashserverinterface.cpp
ViewVC logotype

Contents of /glashctl/lashserverinterface.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download)
Thu Jul 19 21:50:05 2007 UTC (17 years, 3 months ago) by larsl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +1 -1 lines
Upgraded to GPL version 3 or later

1 /****************************************************************************
2 GLASHCtl - a simple tray applet for controlling lashd
3
4 Copyright (C) 2006 Lars Luthman <lars.luthman@gmail.com>
5
6 This program 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 3 of the License, or
9 (at your option) any later version.
10
11 This program 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 this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ****************************************************************************/
20
21 #include <iostream>
22 #include <sstream>
23 #include <unistd.h>
24
25 #include <glibmm.h>
26 #include <sigc++/sigc++.h>
27
28 #include "lashserverinterface.hpp"
29
30
31 using namespace Glib;
32 using namespace sigc;
33 using namespace std;
34
35
36 LASHServerInterface::LASHServerInterface(lash_args_t* args,
37 const string& classname,
38 int timeout) : m_client(NULL) {
39 char* dummy_arg = strdup("thisisnotarealprogramname_q238dnvsd9824fj");
40 char** dummy_argv = &dummy_arg;
41 int dummy_argc = 1;
42 time_t now = time(NULL);
43 do {
44 m_client = lash_init(lash_extract_args(&dummy_argc, &dummy_argv),
45 classname.c_str(), LASH_Server_Interface,
46 LASH_PROTOCOL(2, 0));
47 if (m_client || time(NULL) >= now + timeout)
48 break;
49 ::usleep(500000);
50 } while (time(NULL) < now + timeout);
51
52 m_connection = signal_timeout().
53 connect(mem_fun(*this, &LASHServerInterface::check_events), 200);
54 }
55
56
57 LASHServerInterface::~LASHServerInterface() {
58 m_connection.disconnect();
59 }
60
61
62 bool LASHServerInterface::is_valid() const {
63 return lash_enabled(m_client);
64 }
65
66
67 bool LASHServerInterface::has_session() const {
68 return m_projects.size() > 0;
69 }
70
71
72 void LASHServerInterface::restore_session(const string& directory) {
73 if (is_valid()) {
74 lash_event_t* event = lash_event_new_with_type(LASH_Project_Add);
75 lash_event_set_string(event, directory.c_str());
76 lash_send_event(m_client, event);
77 }
78 }
79
80
81 void LASHServerInterface::set_session_dir(const string& directory) {
82 if (is_valid() && has_session()) {
83 lash_event_t* event = lash_event_new_with_type(LASH_Project_Dir);
84 lash_event_set_string(event, directory.c_str());
85 lash_event_set_project(event, m_projects.begin()->name.c_str());
86 lash_send_event(m_client, event);
87 }
88 }
89
90
91 void LASHServerInterface::save_session() {
92 if (is_valid() && has_session()) {
93 lash_event_t* event = lash_event_new_with_type(LASH_Save);
94 lash_event_set_project(event, m_projects.begin()->name.c_str());
95 lash_send_event(m_client, event);
96 }
97 }
98
99
100 void LASHServerInterface::close_session() {
101 if (is_valid() && has_session()) {
102 lash_event_t* event = lash_event_new_with_type(LASH_Project_Remove);
103 lash_event_set_project(event, m_projects.begin()->name.c_str());
104 lash_send_event(m_client, event);
105 }
106 }
107
108
109 void LASHServerInterface::close_all_sessions() {
110 if (is_valid()) {
111 list<Project>::iterator iter;
112 for (iter = m_projects.begin(); iter != m_projects.end(); ++iter) {
113 lash_event_t* event = lash_event_new_with_type(LASH_Project_Remove);
114 lash_event_set_project(event, m_projects.begin()->name.c_str());
115 lash_send_event(m_client, event);
116 }
117 }
118 }
119
120
121 void LASHServerInterface::rename_session(const string& new_name) {
122 if (is_valid() && has_session()) {
123 lash_event_t* event = lash_event_new_with_type(LASH_Project_Name);
124 lash_event_set_project(event, m_projects.begin()->name.c_str());
125 lash_event_set_string(event, new_name.c_str());
126 lash_send_event(m_client, event);
127 }
128 }
129
130
131 bool LASHServerInterface::check_events() {
132
133 if (!is_valid())
134 return true;
135
136 lash_event_t* event;
137 while ((event = lash_get_event(m_client))) {
138
139 switch (lash_event_get_type(event)) {
140
141 case LASH_Project_Add: {
142 Project p = { lash_event_get_string(event), "" };
143 m_projects.push_front(p);
144 ostringstream oss;
145 oss<<"Project '"<<lash_event_get_string(event)<<"' added";
146 signal_event_received(oss.str());
147 signal_session_changed(lash_event_get_string(event));
148 break;
149 }
150
151 case LASH_Project_Remove: {
152 list<Project>::iterator iter;
153 for (iter = m_projects.begin(); iter != m_projects.end(); ++iter) {
154 if (iter->name == lash_event_get_project(event)) {
155 bool active = false;
156 if (iter == m_projects.begin())
157 active = true;
158 m_projects.erase(iter);
159 ostringstream oss;
160 oss<<"Project '"<<lash_event_get_project(event)<<"' removed";
161 signal_event_received(oss.str());
162 if (has_session())
163 signal_session_changed(m_projects.begin()->name);
164 else
165 signal_session_changed("");
166 break;
167 }
168 }
169 break;
170 }
171
172 case LASH_Project_Dir: {
173 list<Project>::iterator iter;
174 for (iter = m_projects.begin(); iter != m_projects.end(); ++iter) {
175 if (iter->name == lash_event_get_project(event)) {
176 iter->dir = lash_event_get_string(event);
177 ostringstream oss;
178 oss<<"Project '"<<lash_event_get_project(event)
179 <<"' has been moved to '"<<lash_event_get_string(event)<<"'";
180 signal_event_received(oss.str());
181 break;
182 }
183 }
184 break;
185 }
186
187 case LASH_Project_Name: {
188 list<Project>::iterator iter;
189 for (iter = m_projects.begin(); iter != m_projects.end(); ++iter) {
190 if (iter->name == lash_event_get_project(event)) {
191 iter->name = lash_event_get_string(event);
192 ostringstream oss;
193 oss<<"Project '"<<lash_event_get_project(event)
194 <<"' has changed name to '"<<lash_event_get_string(event)<<"'";
195 signal_event_received(oss.str());
196 if (iter == m_projects.begin())
197 signal_session_changed(iter->name);
198 break;
199 }
200 }
201 break;
202 }
203
204 case LASH_Percentage: {
205 ostringstream oss;
206 oss<<"LASH_Percentage event: "<<lash_event_get_project(event)<<" "
207 <<lash_event_get_string(event);
208 signal_event_received(oss.str());
209 if (string("100") == lash_event_get_string(event)) {
210 cerr<<"Project '"<<lash_event_get_project(event)<<"' saved"<<endl;
211 list<Project>::const_iterator iter;
212 for (iter = m_projects.begin(); iter != m_projects.end(); ++iter) {
213 if (iter->name == lash_event_get_project(event)) {
214 signal_session_saved(iter->name, iter->dir);
215 break;
216 }
217 }
218 }
219 break;
220 }
221
222 default:
223 ostringstream oss;
224 oss<<"Got unhandled LASH event from the server";
225 signal_event_received(oss.str());
226 cerr<<oss.str()<<endl;
227 }
228
229 lash_event_destroy(event);
230 }
231
232 return true;
233 }

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26