Main Page   Modules   Data Structures   File List   Data Fields   Globals  

user_interaction.cpp

Go to the documentation of this file.
00001 /*
00002  * user_interaction.cpp
00003  * Created on 25 Feb 2005
00004  * Authors:
00005  *    Wesley Leggette <wleggette@kaylix.net>
00006  * 
00007  * 
00008  * 
00009  * libdarc
00010  * 
00011  * 
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  * 
00026  * $Id: user__interaction_8cpp-source.html 708 2005-03-21 12:30:37Z leggwes $
00027  */
00028 
00035 #include "user_interaction.h"
00036 #include "user_interaction_specific.hpp"
00037 #if LIBDARC_HAVE_LIBDAR_HPP
00038 #include <dar/libdar.hpp>
00039 #endif
00040 
00041 #include <string>
00042 
00043 NAMESPACE_LIBDAR_START
00044 
00045 
00046 extern "C" bool dar_user_interaction_struct_is_valid(dar_user_interaction_struct obj)
00047 {
00048     if ( obj.warning != NULL &&
00049          obj.answer != NULL &&
00050          obj.string != NULL
00051        )
00052         return true;
00053     else
00054         return false;
00055 }
00056 
00057 user_interaction_callback_c::user_interaction_callback_c (dar_user_interaction_struct &callbacks)
00058 {
00059     m_warning = callbacks.warning;
00060     m_answer = callbacks.answer;
00061     m_string = callbacks.string;
00062     m_listing = callbacks.listing;
00063     if (callbacks.listing != NULL)
00064         set_use_listing(true);
00065     context_val = callbacks.context;
00066 }
00067 
00068 
00069 void user_interaction_callback_c::pause(const std::string & message)
00070 {
00071     bool ret = (*m_answer)((char*)message.c_str(), context_val);
00072     if ( !ret )
00073         throw Euser_abort("negative response returned from answer callback");
00074     return;
00075 }
00076 
00077 void user_interaction_callback_c::warning(const std::string & message)
00078 {
00079     (*m_warning)((char*)message.c_str(), context_val);
00080     return;
00081 }
00082 
00083 std::string user_interaction_callback_c::get_string(const std::string & message, bool echo)
00084 {
00085     char* ret = (*m_string)((char*)message.c_str(), echo, context_val);
00086     if (ret == NULL)
00087     {
00088         return std::string("");
00089     } else {
00090         std::string r(ret);
00091         free(ret);
00092         return r;
00093     }
00094 }
00095 void user_interaction_callback_c::listing(
00096         const std::string & flag,
00097         const std::string & perm,
00098         const std::string & uid,
00099         const std::string & gid,
00100         const std::string & size,
00101         const std::string & date,
00102         const std::string & filename,
00103         bool is_dir,
00104         bool has_children)
00105 {
00106     (*m_listing)(   (char*)flag.c_str(),
00107                     (char*)perm.c_str(),
00108                     (char*)uid.c_str(),
00109                     (char*)gid.c_str(),
00110                     (char*)size.c_str(),
00111                     (char*)date.c_str(),
00112                     (char*)filename.c_str(),
00113                     is_dir,
00114                     has_children,
00115                     context_val
00116                 );
00117     return;
00118 }
00119 
00120 user_interaction* user_interaction_callback_c::clone() const
00121 {
00122     dar_user_interaction_struct t;
00123     t.warning = m_warning;
00124     t.answer = m_answer;
00125     t.string = m_string;
00126     t.listing = m_listing;
00127     t.context = context_val;
00128     return new user_interaction_callback_c(t);
00129 }
00130 
00131 /* null user interaction class */
00132 
00133 user_interaction_null::user_interaction_null() {}
00134 
00135 
00136 void user_interaction_null::pause(const std::string & message)
00137 {
00138     return;
00139 }
00140 
00141 void user_interaction_null::warning(const std::string & message)
00142 {
00143     return;
00144 }
00145 
00146 std::string user_interaction_null::get_string(const std::string & message, bool echo)
00147 {
00148     return NULL;
00149 }
00150 void user_interaction_null::listing(
00151         const std::string & flag,
00152         const std::string & perm,
00153         const std::string & uid,
00154         const std::string & gid,
00155         const std::string & size,
00156         const std::string & date,
00157         const std::string & filename,
00158         bool is_dir,
00159         bool has_children)
00160 {
00161     return;
00162 }
00163 
00164 user_interaction* user_interaction_null::clone() const
00165 {
00166     return new user_interaction_null();
00167 }
00168 
00169 
00170 
00171 
00172 NAMESPACE_LIBDAR_END
00173