00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "pages.h"
00037 #include "storage.h"
00038 #include "autoscan.h"
00039 #include "content_manager.h"
00040 #include "filesystem.h"
00041
00042 using namespace zmm;
00043 using namespace mxml;
00044
00045 int WebAutoscanProcessListComparator(void *arg1, void *arg2)
00046 {
00047 AutoscanDirectory *a1 = (AutoscanDirectory *)arg1;
00048 AutoscanDirectory *a2 = (AutoscanDirectory *)arg2;
00049 return strcmp(a1->getLocation().c_str(), a2->getLocation().c_str());
00050 }
00051
00052 web::autoscan::autoscan() : WebRequestHandler()
00053 {
00054 }
00055
00056 void web::autoscan::process()
00057 {
00058
00059 check_request();
00060
00061 String action = param(_("action"));
00062 if (! string_ok(action))
00063 throw _Exception(_("web:autoscan called with illegal action"));
00064
00065 Ref<ContentManager> cm = ContentManager::getInstance();
00066 Ref<Storage> storage = Storage::getInstance();
00067
00068
00069 bool fromFs = boolParam(_("from_fs"));
00070 String path;
00071 String objID = param(_("object_id"));
00072 if (fromFs)
00073 {
00074 if (! string_ok(objID) || objID == "0")
00075 path = _(FS_ROOT_DIRECTORY);
00076 else
00077 path = hex_decode_string(objID);
00078 }
00079
00080 if (action == "as_edit_load")
00081 {
00082 Ref<Element> autoscan (new Element(_("autoscan")));
00083 root->appendElementChild(autoscan);
00084 if (fromFs)
00085 {
00086 autoscan->appendTextChild(_("from_fs"), _("1"), mxml_bool_type);
00087 autoscan->appendTextChild(_("object_id"), objID);
00088 Ref<AutoscanDirectory> adir = cm->getAutoscanDirectory(path);
00089 autoscan2XML(autoscan, adir);
00090 }
00091 else
00092 {
00093 autoscan->appendTextChild(_("from_fs"), _("0"), mxml_bool_type);
00094 autoscan->appendTextChild(_("object_id"), objID);
00095 Ref<AutoscanDirectory> adir = storage->getAutoscanDirectory(intParam(_("object_id")));
00096 autoscan2XML(autoscan, adir);
00097 }
00098 }
00099 else if (action == "as_edit_save")
00100 {
00101 String scan_mode_str = param(_("scan_mode"));
00102 if (scan_mode_str == "none")
00103 {
00104
00105 try
00106 {
00107 if (fromFs)
00108 cm->removeAutoscanDirectory(path);
00109 else
00110 cm->removeAutoscanDirectory(intParam(_("object_id")));
00111 }
00112 catch (Exception e)
00113 {
00114
00115 }
00116 }
00117 else
00118 {
00119
00120 bool recursive = boolParam(_("recursive"));
00121 bool hidden = boolParam(_("hidden"));
00122
00123
00124 scan_mode_t scan_mode = AutoscanDirectory::remapScanmode(scan_mode_str);
00125 scan_level_t scan_level;
00126 scan_level = AutoscanDirectory::remapScanlevel(param(_("scan_level")));
00127 int interval = intParam(_("interval"), 0);
00128 if (scan_mode == TimedScanMode && interval <= 0)
00129 throw _Exception(_("illegal interval given"));
00130
00131 int objectID = INVALID_OBJECT_ID;
00132 if (fromFs)
00133 objectID = cm->ensurePathExistence(path);
00134 else
00135 objectID = intParam(_("object_id"));
00136
00137
00138
00139
00140
00141 Ref<AutoscanDirectory> autoscan(new AutoscanDirectory(
00142 nil,
00143 scan_mode,
00144 scan_level,
00145 recursive,
00146 false,
00147 INVALID_SCAN_ID,
00148 interval,
00149 hidden
00150 ));
00151 autoscan->setObjectID(objectID);
00152 cm->setAutoscanDirectory(autoscan);
00153 }
00154 }
00155 else if (action == "list")
00156 {
00157 Ref<Array<AutoscanDirectory> > autoscanList = cm->getAutoscanDirectories();
00158 int size = autoscanList->size();
00159
00160
00161
00162 quicksort((COMPARABLE *) autoscanList->getObjectArray(), autoscanList->size(),
00163 WebAutoscanProcessListComparator);
00164
00165
00166
00167 Ref<Element> autoscansEl (new Element(_("autoscans")));
00168 autoscansEl->setArrayName(_("autoscan"));
00169 for (int i = 0; i < size; i++)
00170 {
00171 Ref<AutoscanDirectory> autoscanDir = autoscanList->get(i);
00172 Ref<Element> autoscanEl (new Element(_("autoscan")));
00173 autoscanEl->setAttribute(_("objectID"), String::from(autoscanDir->getObjectID()));
00174 autoscanEl->appendTextChild(_("location"), autoscanDir->getLocation());
00175 autoscanEl->appendTextChild(_("scan_mode"), AutoscanDirectory::mapScanmode(autoscanDir->getScanMode()));
00176 autoscanEl->appendTextChild(_("from_config"), autoscanDir->persistent() ? _("1") : _("0"), mxml_bool_type);
00177
00178 autoscansEl->appendElementChild(autoscanEl);
00179 }
00180 root->appendElementChild(autoscansEl);
00181 }
00182 else
00183 throw _Exception(_("web:autoscan called with illegal action"));
00184 }
00185
00186 void web::autoscan::autoscan2XML(Ref<Element> element, Ref<AutoscanDirectory> adir)
00187 {
00188 if (adir == nil)
00189 {
00190 element->appendTextChild(_("scan_mode"), _("none"));
00191 element->appendTextChild(_("scan_level"), _("full"));
00192 element->appendTextChild(_("recursive"), _("0"), mxml_bool_type);
00193 element->appendTextChild(_("hidden"), _("0"), mxml_bool_type);
00194 element->appendTextChild(_("interval"), _("1800"), mxml_int_type);
00195 element->appendTextChild(_("persistent"), _("0"), mxml_bool_type);
00196 }
00197 else
00198 {
00199 element->appendTextChild(_("scan_mode"), AutoscanDirectory::mapScanmode(adir->getScanMode()));
00200 element->appendTextChild(_("scan_level"), AutoscanDirectory::mapScanlevel(adir->getScanLevel()));
00201 element->appendTextChild(_("recursive"), (adir->getRecursive() ? _("1") : _("0") ), mxml_bool_type);
00202 element->appendTextChild(_("hidden"), (adir->getHidden() ? _("1") : _("0") ), mxml_bool_type);
00203 element->appendTextChild(_("interval"), String::from(adir->getInterval()), mxml_int_type);
00204 element->appendTextChild(_("persistent"), (adir->persistent() ? _("1") : _("0") ), mxml_bool_type);
00205 }
00206 }