00001
00002
00003 #include "Iguana/WebFramework/interface/IgWebTreeService.h"
00004 #include "Iguana/Framework/interface/IgState.h"
00005 #include "Iguana/Framework/interface/IgRepSet.h"
00006 #include "Iguana/Framework/interface/IgRepContext.h"
00007 #include "Iguana/Studio/interface/IgDocumentData.h"
00008 #include "Iguana/Framework/interface/IgTwig.h"
00009 #include "Iguana/Models/interface/IgAjaxTreeModel.h"
00010 #include "Iguana/Models/interface/IgUIDModel.h"
00011 #include <classlib/utils/DebugAids.h>
00012 #include <classlib/utils/Log.h>
00013 #include <classlib/utils/StringOps.h>
00014 #include <classlib/utils/Callback.h>
00015 #include <qiodevice.h>
00016 #include <qdom.h>
00017 #include <qtextstream.h>
00018 #include <qdatastream.h>
00019 #include <iostream>
00020 #include <typeinfo>
00021 #include <qapplication.h>
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 IgWebTreeService::IgWebTreeService (IgState *state)
00036 : IgWebService (state),
00037 m_uidModel (new IgUIDModel ())
00038 {
00039 registerCallback ("browse", lat::CreateCallback (this, &IgWebTreeService::browse));
00040 registerCallback ("getRepID", lat::CreateCallback (this, &IgWebTreeService::getRepID));
00041 registerCallback ("twigMenu", lat::CreateCallback (this, &IgWebTreeService::twigMenu));
00042 }
00043
00044 void
00045 IgWebTreeService::getRepID (Arguments *arguments)
00046 {
00047 IgState *state = arguments->state ();
00048 QIODevice *outputDevice = arguments->outputDevice ();
00049 ArgumentsMap &args = *(arguments->args ());
00050
00051 IgDocumentData *dd = IgDocumentData::get (state);
00052 ASSERT (dd);
00053
00054 std::string documentName = "";
00055
00056 if (args.find ("doc") != args.end ())
00057 {
00058 documentName = args["doc"];
00059 }
00060
00061 IgTwig *twig = dynamic_cast <IgTwig *> (dd->root (documentName));
00062
00063 if (twig)
00064 {
00065 if (args.find ("t") == args.end ())
00066 {
00067 this->sendError (outputDevice);
00068 }
00069
00070 IgTwig *target = dynamic_cast <IgTwig *> (twig->lookup (args["t"]));
00071 ASSERT (target);
00072 IgUIDRep *uidRep
00073 = dynamic_cast <IgUIDRep *> (IgRepSet::lookup (target,
00074 m_uidModel,
00075 true));
00076 ASSERT (uidRep);
00077 QTextStream os (outputDevice);
00078
00079 os << "HTTP/1.1 200 Ok\r\n"
00080 "content-type: text/xml; charset=UTF-8\r\n"
00081 "\r\n"
00082 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
00083 << "<uid>" << uidRep->id () << "</uid>"
00084 << "\r\n";
00085 }
00086 }
00087
00088 void
00089 IgWebTreeService::browse (Arguments *arguments)
00090 {
00091 qApp->lock ();
00092
00093 IgState *state = arguments->state ();
00094 QIODevice *outputDevice = arguments->outputDevice ();
00095 ArgumentsMap &args = *(arguments->args ());
00096
00097 IgDocumentData *dd = IgDocumentData::get (state);
00098
00099 std::string documentName = "";
00100
00101 if (args.find ("doc") != args.end ())
00102 {
00103 documentName = args["doc"];
00104 }
00105
00106 IgTwig *twig = dynamic_cast <IgTwig *> (dd->root (documentName));
00107
00108 if (twig)
00109 {
00110 twig->selfTraverse (true);
00111 IgAjaxTreeModel model (m_uidModel);
00112
00113 IgTwig *target;
00114
00115 if (args.find ("i") != args.end ())
00116 {
00117 IgUIDRep * rep = m_uidModel->lookup (atoi (args["i"].c_str ()));
00118 ASSERT (rep);
00119 target = dynamic_cast <IgTwig *> (rep->context ()->object ());
00120 ASSERT (target);
00121 }
00122 else if (args.find ("t") != args.end ())
00123 {
00124 target = dynamic_cast <IgTwig *> (twig->lookup (args["t"]));
00125 ASSERT (target);
00126 }
00127 else
00128 {
00129 this->sendError (outputDevice);
00130 return;
00131 }
00132
00133
00134 ASSERT (target);
00135
00136 target->selfTraverse (true);
00137
00138 if (args.find ("setVisible") != args.end ())
00139 {
00140 if (args["setVisible"] == "1")
00141 {
00142 target->selfVisible (true);
00143 }
00144 else
00145 {
00146 target->selfVisible (false);
00147 }
00148 }
00149
00150 IgRepSet::lookup (target, &model, true);
00151 IgRepSet::update (target, IgTwig::SELF_MASK
00152 | IgTwig::STRUCTURE_MASK
00153 | IgTwig::FLAGS_MASK);
00154 target->expand ();
00155
00156 IgRepSet::lookup (target, &model, true);
00157
00158 for (unsigned int i = 0;
00159 i < target->children ();
00160 i++)
00161 {
00162 IgRepSet::lookup (target->child (i),
00163 &model,
00164 true);
00165 }
00166
00167 QTextStream os (outputDevice);
00168
00169 os << "HTTP/1.1 200 Ok\r\n"
00170 "content-type: text/xml; charset=UTF-8\r\n"
00171 "\r\n"
00172 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
00173 << model.document ()->toString () << "\r\n";
00174
00175 std::cerr << model.document ()->toString () << std::endl;
00176 }
00177 qApp->unlock ();
00178
00179 }
00180
00181 void
00182 IgWebTreeService::twigMenu (Arguments *arguments)
00183 {
00184 QIODevice *outputDevice = arguments->outputDevice ();
00185 ArgumentsMap &args = *(arguments->args ());
00186
00187 if (args.find ("i") == args.end ())
00188 return;
00189
00190 IgUIDRep *rep = m_uidModel->lookup (atoi (args["i"].c_str ()));
00191 ASSERT (rep);
00192
00193 IgRepresentable *object = rep->context ()->object ();
00194
00195 for (MenuHooks::iterator i = m_menuHooks.begin ();
00196 i != m_menuHooks.end ();
00197 i++)
00198 {
00199 std::string xmlMenuDescription = (*i)(object);
00200 if (xmlMenuDescription != "")
00201 {
00202 this->sendXML (outputDevice, xmlMenuDescription);
00203 return;
00204 }
00205 }
00206
00207 this->sendXML (outputDevice, "<menu name=\"default\"/>");
00208 }
00209
00210 void
00211 IgWebTreeService::registerMenuHook (MenuHook hook)
00212 {
00213 m_menuHooks.push_back (hook);
00214 }
00215
00216
00217 IgUIDModel *
00218 IgWebTreeService::uidModel (void)
00219 {
00220 return m_uidModel;
00221 }
00222
00223 const char *
00224 IgWebTreeService::catalogLabel (void)
00225 {
00226 return "Tree";
00227 }