Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-studio / trunk / src / gtkmm / autorecover.cpp
index 06c1db3..1b55eb4 100644 (file)
@@ -1,20 +1,22 @@
-/* === S I N F G =========================================================== */
+/* === S Y N F I G ========================================================= */
 /*!    \file autorecover.cpp
 **     \brief Template File
 **
-**     $Id: autorecover.cpp,v 1.1.1.1 2005/01/07 03:34:35 darco Exp $
+**     $Id$
 **
 **     \legal
-**     Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2008 Chris Moore
 **
-**     This software and associated documentation
-**     are CONFIDENTIAL and PROPRIETARY property of
-**     the above-mentioned copyright holder.
+**     This package is free software; you can redistribute it and/or
+**     modify it under the terms of the GNU General Public License as
+**     published by the Free Software Foundation; either version 2 of
+**     the License, or (at your option) any later version.
 **
-**     You may not copy, print, publish, or in any
-**     other way distribute this software without
-**     a prior written agreement with
-**     the copyright holder.
+**     This package 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
+**     General Public License for more details.
 **     \endlegal
 */
 /* ========================================================================= */
 
 #include "autorecover.h"
 
+#ifdef HAVE_SYS_ERRNO_H
+#include <sys/errno.h>
+#endif
 //#include <unistd.h>
 #include "app.h"
-#include <sinfg/savecanvas.h>
-#include <sinfg/loadcanvas.h>
+#include <synfig/savecanvas.h>
+#include <synfig/loadcanvas.h>
 #include <fstream>
 #include <iostream>
 #include "instance.h"
 #include <unistd.h>
 #endif
 
+#include "general.h"
+
 #endif
 
 /* === U S I N G =========================================================== */
 
 using namespace std;
 using namespace etl;
-using namespace sinfg;
+using namespace synfig;
 using namespace studio;
 
 /* === M A C R O S ========================================================= */
@@ -83,15 +90,15 @@ AutoRecover::AutoRecover()
 {
        // Three Minutes
        set_timeout(3*60*1000);
-       
+
        if(mkdir(get_shadow_directory().c_str(),ACCESSPERMS)<0)
        {
                if(errno!=EEXIST)
-                       sinfg::error("UNABLE TO CREATE \"%s\"",get_shadow_directory().c_str());
+                       synfig::error("UNABLE TO CREATE \"%s\"",get_shadow_directory().c_str());
        }
        else
        {
-               sinfg::info("Created directory \"%s\"",get_shadow_directory().c_str());
+               synfig::info("Created directory \"%s\"",get_shadow_directory().c_str());
        }
 }
 
@@ -99,7 +106,7 @@ AutoRecover::~AutoRecover()
 {
 }
 
-sinfg::String
+synfig::String
 AutoRecover::get_shadow_directory()
 {
        return Glib::build_filename(App::get_user_app_directory(),"tmp");
@@ -122,18 +129,18 @@ AutoRecover::set_timeout(int milliseconds)
 //             auto_backup_connect=App::main.get_context()->signal_timeout().connect(sigc::mem_fun(&AutoRecover::auto_backup),timeout);
 }
 
-sinfg::String
-AutoRecover::get_shadow_file_name(const sinfg::String& filename)
+synfig::String
+AutoRecover::get_shadow_file_name(const synfig::String& filename)
 {
        unsigned int hash1(0xdeadbeef);
        unsigned int hash2(0x83502529);
        char* str_hash1(reinterpret_cast<char*>(&hash1));
        char* str_hash2(reinterpret_cast<char*>(&hash2));
-       
+
        // First we need to hash up the directory
        {
                String pool(dirname(filename));
-               
+
                while(pool.size()>4)
                {
                        str_hash1[0]^=pool[1];str_hash1[1]^=pool[2];str_hash1[2]^=pool[3];str_hash1[3]^=pool[0];
@@ -152,10 +159,10 @@ AutoRecover::get_shadow_file_name(const sinfg::String& filename)
                }
        }
        hash1^=hash2;
-       
+
        return Glib::build_filename(get_shadow_directory(),strprintf("%08X-%s",hash1,basename(filename).c_str()));
-       
-//     return dirname(filename) + ETL_DIRECTORY_SEPERATOR + ".shadow_" + basename(filename);
+
+//     return dirname(filename) + ETL_DIRECTORY_SEPARATOR + ".shadow_" + basename(filename);
 }
 
 bool
@@ -165,15 +172,15 @@ AutoRecover::cleanup_pid(int pid)
        int status=0;
        if(waitpid(pid,&status,WNOHANG)==-1)
        {
-               sinfg::info("PID %d isn't a zombie yet",pid);
+               synfig::info("PID %d isn't a zombie yet",pid);
                return true;
        }
        if(WEXITSTATUS(status)!=0)
        {
-               sinfg::error("Autobackup seems to have failed! (PID=%d)",pid);
+               synfig::error("Autobackup seems to have failed! (PID=%d)",pid);
        }
-       else
-               sinfg::info("PID=%d has been cleaned up",pid);
+//     else
+//             synfig::info("PID=%d has been cleaned up",pid);
 #endif
        return false;
 }
@@ -186,7 +193,7 @@ AutoRecover::auto_backup()
 #ifdef HAVE_FORK
        pid=fork();
 #endif
-       
+
        if(pid<=0)
        {
 #ifdef HAVE_SETPRIORITY
@@ -194,16 +201,16 @@ AutoRecover::auto_backup()
                // cause the machine to slow down too much
                setpriority(PRIO_PROCESS,0,15);
 #endif
-               
+
                try
                {
                        std::list<etl::handle<Instance> >::iterator iter;
-       
+
                        std::string filename=App::get_config_file("autorecovery");
                        std::ofstream file(filename.c_str());
-                       
+
                        int savecount(0);
-                       
+
                        for(iter=App::instance_list.begin();iter!=App::instance_list.end();++iter)
                        {
                                // If this file hasn't even been changed
@@ -211,22 +218,22 @@ AutoRecover::auto_backup()
                                // backing it up.
                                if((*iter)->get_action_count()==0)
                                        continue;
-                                       
+
                                Canvas::Handle canvas((*iter)->get_canvas());
                                file<<canvas->get_file_name()<<endl;
                                save_canvas(get_shadow_file_name(canvas->get_file_name()),canvas);
                                savecount++;
                        }
-                       
-                       if(savecount)
-                               sinfg::info("AutoRecover::auto_backup(): %d Files backed up.",savecount);
+
+//                     if(savecount)
+//                             synfig::info("AutoRecover::auto_backup(): %d Files backed up.",savecount);
                }
                catch(...)
                {
-                       sinfg::error("AutoRecover::auto_backup(): UNKNOWN EXCEPTION THROWN.");
-                       sinfg::error("AutoRecover::auto_backup(): FILES NOT BACKED UP.");
+                       synfig::error("AutoRecover::auto_backup(): UNKNOWN EXCEPTION THROWN.");
+                       synfig::error("AutoRecover::auto_backup(): FILES NOT BACKED UP.");
                }
-               
+
 #ifdef HAVE_FORK
                if(pid==0)
                {
@@ -243,11 +250,11 @@ AutoRecover::auto_backup()
                ),
                60*1000
        );
-#endif 
-       
+#endif
+
        // Also go ahead and save the settings
        App::save_settings();
-       
+
        return true;
 }
 
@@ -266,19 +273,20 @@ AutoRecover::recovery_needed()const
                if(!filename.empty())
                        return true;
        }
-       
+
        return false;
 }
 
 bool
-AutoRecover::recover()
+AutoRecover::recover(int& number_recovered)
 {
        std::string filename=App::get_config_file("autorecovery");
        std::ifstream file(filename.c_str());
+       number_recovered = 0;
        if(!file)
                return false;
        bool success=true;
-       
+
        while(file)
        {
                std::string filename;
@@ -291,9 +299,11 @@ AutoRecover::recover()
                {
                        // Correct the file name
                        App::instance_list.back()->set_file_name(filename);
-                       
+
                        // This file isn't saved! mark it as such
                        App::instance_list.back()->inc_action_count();
+
+                       number_recovered++;
                }
                else
                        success=false;
@@ -313,7 +323,7 @@ AutoRecover::normal_shutdown()
 }
 
 void
-AutoRecover::clear_backup(sinfg::Canvas::Handle canvas)
+AutoRecover::clear_backup(synfig::Canvas::Handle canvas)
 {
        if(canvas)
                remove(get_shadow_file_name(canvas->get_file_name()).c_str());