1 /* === S Y N F I G ========================================================= */
2 /*! \file autorecover.cpp
3 ** \brief Template File
5 ** $Id: autorecover.cpp,v 1.1.1.1 2005/01/07 03:34:35 darco Exp $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "autorecover.h"
36 #include <synfig/savecanvas.h>
37 #include <synfig/loadcanvas.h>
42 #ifdef HAVE_SYS_STAT_H
46 #ifdef HAVE_SYS_TIME_H
50 #ifdef HAVE_SYS_WAIT_H
54 #ifdef HAVE_SYS_RESOURCE_H
55 #include <sys/resource.h>
64 /* === U S I N G =========================================================== */
68 using namespace synfig;
69 using namespace studio;
71 /* === M A C R O S ========================================================= */
74 #define mkdir(x,y) mkdir(x)
77 /* === G L O B A L S ======================================================= */
79 /* === P R O C E D U R E S ================================================= */
81 /* === M E T H O D S ======================================================= */
83 AutoRecover::AutoRecover()
86 set_timeout(3*60*1000);
88 if(mkdir(get_shadow_directory().c_str(),ACCESSPERMS)<0)
91 synfig::error("UNABLE TO CREATE \"%s\"",get_shadow_directory().c_str());
95 synfig::info("Created directory \"%s\"",get_shadow_directory().c_str());
99 AutoRecover::~AutoRecover()
104 AutoRecover::get_shadow_directory()
106 return Glib::build_filename(App::get_user_app_directory(),"tmp");
117 AutoRecover::set_timeout(int milliseconds)
119 timeout=milliseconds;
120 auto_backup_connect.disconnect();
122 auto_backup_connect=Glib::signal_timeout().connect(sigc::ptr_fun(&AutoRecover::auto_backup),timeout);
123 // auto_backup_connect=App::main.get_context()->signal_timeout().connect(sigc::mem_fun(&AutoRecover::auto_backup),timeout);
127 AutoRecover::get_shadow_file_name(const synfig::String& filename)
129 unsigned int hash1(0xdeadbeef);
130 unsigned int hash2(0x83502529);
131 char* str_hash1(reinterpret_cast<char*>(&hash1));
132 char* str_hash2(reinterpret_cast<char*>(&hash2));
134 // First we need to hash up the directory
136 String pool(dirname(filename));
140 str_hash1[0]^=pool[1];str_hash1[1]^=pool[2];str_hash1[2]^=pool[3];str_hash1[3]^=pool[0];
141 str_hash2[3]+=pool[0];str_hash2[2]+=pool[1];str_hash2[1]+=pool[2];str_hash2[0]+=pool[3];
143 pool=String(pool,4,pool.size());
147 str_hash1[0]^=pool[0];
148 str_hash1[2]^=pool[0];
149 str_hash2[1]^=pool[0];
150 str_hash2[3]^=pool[0];
152 pool=String(pool,1,pool.size());
157 return Glib::build_filename(get_shadow_directory(),strprintf("%08X-%s",hash1,basename(filename).c_str()));
159 // return dirname(filename) + ETL_DIRECTORY_SEPERATOR + ".shadow_" + basename(filename);
163 AutoRecover::cleanup_pid(int pid)
167 if(waitpid(pid,&status,WNOHANG)==-1)
169 synfig::info("PID %d isn't a zombie yet",pid);
172 if(WEXITSTATUS(status)!=0)
174 synfig::error("Autobackup seems to have failed! (PID=%d)",pid);
177 synfig::info("PID=%d has been cleaned up",pid);
183 AutoRecover::auto_backup()
193 #ifdef HAVE_SETPRIORITY
194 // make us low priority so that we don't
195 // cause the machine to slow down too much
196 setpriority(PRIO_PROCESS,0,15);
201 std::list<etl::handle<Instance> >::iterator iter;
203 std::string filename=App::get_config_file("autorecovery");
204 std::ofstream file(filename.c_str());
208 for(iter=App::instance_list.begin();iter!=App::instance_list.end();++iter)
210 // If this file hasn't even been changed
211 // since it was last saved, then don't bother
213 if((*iter)->get_action_count()==0)
216 Canvas::Handle canvas((*iter)->get_canvas());
217 file<<canvas->get_file_name()<<endl;
218 save_canvas(get_shadow_file_name(canvas->get_file_name()),canvas);
223 synfig::info("AutoRecover::auto_backup(): %d Files backed up.",savecount);
227 synfig::error("AutoRecover::auto_backup(): UNKNOWN EXCEPTION THROWN.");
228 synfig::error("AutoRecover::auto_backup(): FILES NOT BACKED UP.");
240 Glib::signal_timeout().connect(
242 sigc::ptr_fun(&AutoRecover::cleanup_pid),
249 // Also go ahead and save the settings
250 App::save_settings();
256 AutoRecover::recovery_needed()const
258 std::string filename=App::get_config_file("autorecovery");
259 std::ifstream file(filename.c_str());
265 std::string filename;
266 getline(file,filename);
267 if(!filename.empty())
275 AutoRecover::recover()
277 std::string filename=App::get_config_file("autorecovery");
278 std::ifstream file(filename.c_str());
285 std::string filename;
286 getline(file,filename);
291 if(App::open_as(get_shadow_file_name(filename),filename))
293 // Correct the file name
294 App::instance_list.back()->set_file_name(filename);
296 // This file isn't saved! mark it as such
297 App::instance_list.back()->inc_action_count();
307 AutoRecover::normal_shutdown()
309 // Turn off the timer
310 auto_backup_connect.disconnect();
312 std::string filename=App::get_config_file("autorecovery");
313 remove(filename.c_str());
317 AutoRecover::clear_backup(synfig::Canvas::Handle canvas)
320 remove(get_shadow_file_name(canvas->get_file_name()).c_str());