Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_06 / src / synfigapp / cvs.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file cvs.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "cvs.h"
33 #include <ETL/stringf>
34 #include <fstream>
35 #include <iostream>
36 #include <synfig/general.h>
37 #include <stdlib.h>
38
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 //#include <unistd.h>
43
44 #include <cassert>
45
46 #endif
47
48 /* === U S I N G =========================================================== */
49
50 using namespace std;
51 using namespace etl;
52 using namespace synfig;
53 using namespace synfigapp;
54
55 /* === M A C R O S ========================================================= */
56
57 #define cvs_command             synfig::String("cvs -z4")
58
59 #ifndef WIN32
60 #define HAVE_STRPTIME
61 #endif
62
63 #ifdef __APPLE__
64 time_t _daylight_() { time_t t(time(0)); return localtime(&t)->tm_gmtoff; }
65 #define daylight _daylight_()
66 #endif
67
68 /* === G L O B A L S ======================================================= */
69
70 /* === P R O C E D U R E S ================================================= */
71
72 /* === M E T H O D S ======================================================= */
73
74 CVSInfo::CVSInfo(const synfig::String& file_name)
75 {
76         update_available_=false;
77         set_file_name(file_name);
78 }
79
80 CVSInfo::CVSInfo()
81 {
82         update_available_=false;
83 }
84
85 CVSInfo::~CVSInfo()
86 {
87 }
88
89 void
90 CVSInfo::set_file_name(const synfig::String& file_name)
91 {
92         file_name_=file_name;
93
94         std::ifstream file((dirname(file_name_)+"/CVS/Root").c_str());
95
96         if(file)
97         {
98                 in_sandbox_=true;
99                 calc_repository_info();
100         }
101         else
102                 in_sandbox_=false;
103 }
104
105 void
106 CVSInfo::calc_repository_info()
107 {
108 #ifdef _DEBUG
109         synfig::info("in_sandbox() = %d",in_sandbox());
110 #endif
111
112         if(!in_sandbox_)
113                 return;
114
115         std::ifstream file((dirname(file_name_)+"/CVS/Entries").c_str());
116
117         while(file)
118         {
119                 String line;
120                 getline(file,line);
121                 if(line.find(basename(file_name_))!=String::npos)
122                 {
123                         in_repository_=true;
124                         String::size_type s,f;
125
126                         // Grab the version
127                         s=line.find('/',1);
128                         assert(s!=String::npos);
129                         s++;
130                         f=line.find('/',s+1);
131                         assert(f!=String::npos);
132                         cvs_version_=String(line,s,f-s);
133
134                         // Grab the time
135 #ifdef HAVE_STRPTIME
136                         s=f+1;
137                         f=line.find('/',s+1);
138                         assert(f!=String::npos);
139                         tm time_struct;
140                         strptime(String(line,s,f-s).c_str(),"%c",&time_struct);
141                         original_timestamp_=mktime(&time_struct);
142 #endif
143
144                         if(
145                                 system(strprintf(
146                                         "cd '%s' && cvs status '%s' | grep -q -e 'Needs Patch'",
147                                         dirname(file_name_).c_str(),
148                                         basename(file_name_).c_str()
149                                 ).c_str())==0
150                         )
151                         {
152                                 synfig::info("UPDATE_AVAILABLE=TRUE");
153                                 update_available_=true;
154                         }
155                         else
156                         {
157                                 system(strprintf(
158                                         "cd '%s' && cvs status '%s'",
159                                         dirname(file_name_).c_str(),
160                                         basename(file_name_).c_str()
161                                 ).c_str());
162                                 synfig::info("UPDATE_AVAILABLE=FALSE");
163                                 update_available_=false;
164                         }
165
166
167 #ifdef _DEBUG
168                         synfig::info("in_repository() = %d",in_repository());
169                         synfig::info("get_cvs_version() = %s",get_cvs_version().c_str());
170                         synfig::info("get_original_timestamp() = %s",ctime(&get_original_timestamp()));
171                         time_t t(get_current_timestamp());
172                         synfig::info("get_current_timestamp() = %s",ctime(&t));
173                         synfig::info("get_cvs_root() = %s",get_cvs_root().c_str());
174                         synfig::info("get_cvs_module() = %s",get_cvs_module().c_str());
175 #endif
176                         return;
177                 }
178         }
179
180         in_repository_=false;
181         cvs_version_.clear();
182         original_timestamp_=0;
183
184 #ifdef _DEBUG
185         synfig::info("in_repository() = %d",in_repository());
186 #endif
187 }
188
189 bool
190 CVSInfo::in_sandbox()const
191 {
192         return in_sandbox_;
193 }
194
195 bool
196 CVSInfo::in_repository()const
197 {
198         if(!in_sandbox_)
199                 return false;
200         return in_repository_;
201 }
202
203 bool
204 CVSInfo::is_modified()const
205 {
206         if(!in_sandbox() || !in_repository())
207                 return false;
208 #ifdef _DEBUG
209         synfig::info("%d-%d=%d",get_current_timestamp(),get_original_timestamp(),get_current_timestamp()-get_original_timestamp());
210 #endif
211         return get_current_timestamp()!=get_original_timestamp() && abs(get_current_timestamp()-get_original_timestamp())!=3600;
212 }
213
214 bool
215 CVSInfo::is_updated()const
216 {
217         return update_available_;
218 }
219
220 const synfig::String&
221 CVSInfo::get_cvs_version()const
222 {
223         return cvs_version_;
224 }
225
226 const time_t&
227 CVSInfo::get_original_timestamp()const
228 {
229         return original_timestamp_;
230 }
231
232 time_t
233 CVSInfo::get_current_timestamp()const
234 {
235         struct stat st;
236         if(stat(file_name_.c_str(),&st)<0)
237         {
238                 synfig::error("Unable to get file stats");
239                 return false;
240         }
241         time_t ret((daylight-1)*3600);
242         //ret+=timezone;
243         ret+=st.st_mtime;
244         return ret;
245 }
246
247 synfig::String
248 CVSInfo::get_cvs_root()const
249 {
250         if(!in_sandbox_)
251                 return synfig::String();
252
253         std::ifstream file((dirname(file_name_)+"/CVS/Root").c_str());
254
255         if(file)
256         {
257                 String ret;
258                 getline(file,ret);
259                 return ret;
260         }
261
262         return synfig::String();
263 }
264
265 synfig::String
266 CVSInfo::get_cvs_module()const
267 {
268         if(!in_sandbox_)
269                 return synfig::String();
270
271         std::ifstream file((dirname(file_name_)+"/CVS/Repository").c_str());
272
273         if(file)
274         {
275                 String ret;
276                 getline(file,ret);
277                 return ret;
278         }
279
280         return synfig::String();
281 }
282
283 // This function pre-processes the message so that we
284 // don't get any CVS syntax errors.
285 inline synfig::String fix_msg(const synfig::String& message)
286 {
287         synfig::String ret;
288         int i;
289         for(i=0;i<(int)message.size();i++)
290         {
291                 if(message[i]=='\'')
292                         ret+="'\"'\"'";
293                 else
294                         ret+=message[i];
295         }
296         return ret;
297 }
298
299 void
300 CVSInfo::cvs_add(const synfig::String& message)
301 {
302         if(!in_sandbox_)
303         {
304                 synfig::error("cvs_add(): Not in a sand box");
305                 throw int();
306                 return;
307         }
308
309         synfig::String command(strprintf("cd '%s' && %s add -m '%s' '%s'",dirname(file_name_).c_str(),cvs_command.c_str(),fix_msg(message).c_str(),basename(file_name_).c_str()));
310
311         int ret(system(command.c_str()));
312
313         calc_repository_info();
314
315         switch(ret)
316         {
317         case 0:
318                 break;
319         default:
320                 synfig::error("Unknown errorcode %d (\"%s\")",ret,command.c_str());
321                 throw(ret);
322                 break;
323         }
324 }
325
326 void
327 CVSInfo::cvs_update()
328 {
329         if(!in_sandbox_)
330         {
331                 synfig::error("cvs_update(): Not in a sand box");
332                 throw int();
333                 return;
334         }
335
336         synfig::String command(strprintf("cd '%s' && %s update '%s'",dirname(file_name_).c_str(),cvs_command.c_str(),basename(file_name_).c_str()));
337
338         int ret(system(command.c_str()));
339
340         calc_repository_info();
341
342         switch(ret)
343         {
344         case 0:
345                 break;
346         default:
347                 synfig::error("Unknown errorcode %d (\"%s\")",ret,command.c_str());
348                 throw(ret);
349                 break;
350         }
351 }
352
353 void
354 CVSInfo::cvs_commit(const synfig::String& message)
355 {
356         if(!in_sandbox_)
357         {
358                 synfig::error("cvs_commit(): Not in a sand box");
359                 throw int();
360                 return;
361         }
362
363         synfig::String command(strprintf("cd '%s' && %s commit -m '%s' '%s'",dirname(file_name_).c_str(),cvs_command.c_str(),fix_msg(message).c_str(),basename(file_name_).c_str()));
364
365         int ret(system(command.c_str()));
366
367         calc_repository_info();
368
369         switch(ret)
370         {
371         case 0:
372                 break;
373         default:
374                 synfig::error("Unknown errorcode %d (\"%s\")",ret,command.c_str());
375                 if(is_modified())
376                         throw(ret);
377                 break;
378         }
379 }