- changed name of package library to package.dll
[reactos.git] / rosapps / packmgr / lib / package.cpp
index 9608168..857fbbd 100644 (file)
-////////////////////////////////////////////////////////\r
-//\r
-// package.cpp\r
-// \r
-// package related functions\r
-//\r
-//\r
-// Maarten Bosma, 09.01.2004\r
-// maarten.paul@bosma.de\r
-//\r
-////////////////////////////////////////////////////////////////////\r
-\r
-#include "package.hpp"\r
-#include "expat.h"\r
-#include "log.h"\r
-\r
-int PML_XmlDownload (const char* url, void* usrdata, XML_StartElementHandler start, \r
-                                        XML_EndElementHandler end, XML_CharacterDataHandler text=0);\r
-\r
-\r
-// expat callback for start of a package tag\r
-void pack_start (void* usrdata, const char* tag, const char** arg)\r
-{\r
-       int i, id;\r
-       PACKAGE* pack = (PACKAGE*)usrdata;\r
-\r
-       // if the tag is a script tag ...\r
-       if(!strcmp(tag, "scripts"))\r
-       {\r
-               // ... read the arguments\r
-               for (i=0; arg[i]; i+=2) \r
-               {\r
-                       if(!strcmp(arg[i], "inst"))\r
-                               id = 0;\r
-\r
-                       else if(!strcmp(arg[i], "update"))\r
-                               id = 1;\r
-\r
-                       else if(!strcmp(arg[i], "uinst"))\r
-                               id = 2;\r
-\r
-                       else if(!strcmp(arg[i], "srcinst"))\r
-                               id = 3;\r
-\r
-                       else\r
-                               continue;\r
-\r
-                       pack->files[id] = new char [strlen(arg[i+1])+1];\r
-                       strcpy(pack->files[id], arg[i+1]);\r
-               }\r
-       }\r
-\r
-       // ... save the field\r
-       else\r
-       {\r
-               if(!strcmp(tag, "name"))\r
-                       pack->field = &pack->name;\r
-\r
-               else if(!strcmp(tag, "description"))\r
-                       pack->field = &pack->description;\r
-       }\r
-}\r
-\r
-// expat callback for end of a package tag\r
-void pack_end (void* usrdata, const char* tag)\r
-{\r
-       PACKAGE* pack = (PACKAGE*)usrdata;\r
-\r
-       pack->field = NULL;\r
-}\r
-\r
-// expat callback for text\r
-void pack_text (void* usrdata, const char* data, int len)\r
-{\r
-       PACKAGE* pack = (PACKAGE*)usrdata;\r
-\r
-       if(!pack->field)\r
-               return;\r
-\r
-       *pack->field = new char[len+1];\r
-       strncpy(*pack->field, data, len);\r
-       (*pack->field)[len] = '\0';\r
-}\r
-\r
-\r
-// The user clicks on a package\r
-extern "C" int PML_LoadPackage (TREE* tree, int id, PML_SetButton SetButton)\r
-{\r
-       PACKAGE* pack = &tree->packages[id];\r
-       tree->setButton = SetButton;\r
-\r
-       if(SetButton)\r
-       {\r
-               SetButton(1, pack->action);\r
-               SetButton(2, pack->inst); // && pack->action != 0\r
-               SetButton(3, pack->src_inst); \r
-               SetButton(4, pack->update);\r
-               SetButton(5, pack->uninstall); \r
-       }\r
-\r
-       // root notes (like network) return here\r
-       if(!pack->path)\r
-               return 1;\r
-\r
-       if(!pack->loaded)\r
-       {\r
-               PML_XmlDownload (pack->path, (void*)pack, pack_start, pack_end, pack_text);\r
-               pack->loaded = TRUE;\r
-       }\r
-\r
-       return ERR_OK;\r
-}\r
-\r
-extern "C" int PML_FindItem (TREE* tree, const char* what)\r
-{\r
-       int i, j;\r
-       bool found;\r
-\r
-       // if we have children, same action for them\r
-       for (i=1; (UINT)i<tree->packages.size(); i++)\r
-       {\r
-               found = true;\r
-\r
-               for(j=0; (UINT)j<strlen(what); j++)\r
-               {\r
-                       if(tolower(what[j]) != tolower(tree->packages[i].name[j]))\r
-                       {\r
-                               found = false;\r
-                               break;\r
-                       }\r
-               }\r
-\r
-               if(found)\r
-                       return i;\r
-       }\r
-\r
-       return 0;\r
-}\r
-\r
-// The user chooses a actions like Install\r
-extern "C" int PML_SetAction (TREE* tree, int id, int action, PML_SetIcon SetIcon)\r
-{\r
-       UINT i;\r
-       int ret = ERR_OK;\r
-\r
-       tree->setIcon = SetIcon;\r
-       PACKAGE* pack = &tree->packages[id];\r
-\r
-       // if we have children, same action for them\r
-       for (i=0; i<pack->children.size(); i++)\r
-               ret = ret || PML_SetAction(tree, pack->children[i], action, SetIcon);\r
-\r
-       // is the action possible ? \r
-       if(!pack->actions[action])\r
-               return ERR_GENERIC;\r
-\r
-       // is it already set \r
-       if(pack->action == action)\r
-               return ERR_OK;\r
-\r
-       // set the icon\r
-       if(!pack->icon)\r
-               if(SetIcon)\r
-                       SetIcon(id, action);\r
-\r
-       // can't do src install yet\r
-       if(action == 2)\r
-       {\r
-               MessageBox(0, L"Sorry, but source install is not implemented yet.", 0,0);\r
-               return ERR_OK;\r
-       }\r
-\r
-       // everything but undoing is done here\r
-       else if (action != 0)\r
-       {\r
-               // since we are setting a action we undo it again\r
-               if(tree->setButton)\r
-                       tree->setButton(1, 1);\r
-               //tree->setButton(action+1, 0);\r
-               pack->action = action;\r
-\r
-               // root notes (like network) return here\r
-               if(!pack->path)\r
-                       return ret; \r
-\r
-               // load it if it's not loaded yet\r
-               if(!pack->loaded)\r
-               {\r
-                       PML_XmlDownload (pack->path, (void*)pack, pack_start, pack_end, pack_text);\r
-                       pack->loaded = TRUE;\r
-               }\r
-\r
-               // save the name of the corresponding script in a vector\r
-               tree->todo.push_back(pack->files[action-1]);\r
-       }\r
-\r
-       // undoing\r
-       else \r
-       {\r
-               // set other things back\r
-               if(tree->setButton)\r
-                       tree->setButton(1, 0);\r
-               //tree->setButton(pack->action+1, 1);\r
-               pack->action = 0;\r
-\r
-               // root notes (like network) return here\r
-               if(!pack->path || pack->action==0)\r
-                       return ret; \r
-       \r
-               // erase from todo list\r
-               for(i=0; i<tree->todo.size(); i++)\r
-                       if(!strcmp(tree->todo[i], pack->files[pack->action-1])) // look for right entry\r
-                               tree->todo.erase(tree->todo.begin()+i); // delete it\r
-\r
-               return ERR_OK;\r
-       }\r
-\r
-       return ret;\r
-}\r
-\r
-//\r
-extern "C" char* PML_GetDescription (TREE* tree, int id)\r
-{\r
-       PML_LoadPackage(tree, id, NULL);\r
-\r
-       return tree->packages[id].description;\r
-}\r
-\r
+////////////////////////////////////////////////////////
+//
+// package.cpp
+// 
+// package related functions
+//
+//
+// Maarten Bosma, 09.01.2004
+// maarten.paul@bosma.de
+//
+////////////////////////////////////////////////////////////////////
+
+#include "package.hpp"
+#include "expat.h"
+#include "log.h"
+
+int PML_XmlDownload (pTree, const char* url, void* usrdata, XML_StartElementHandler start, 
+                                                                       XML_EndElementHandler end, XML_CharacterDataHandler text=0);
+
+
+// expat callback for start of a package tag
+void pack_start (void* usrdata, const char* tag, const char** arg)
+{
+       int i, id;
+       PACKAGE* pack = (PACKAGE*)usrdata;
+
+       // if the tag is a script tag ...
+       if(!strcmp(tag, "scripts"))
+       {
+               // ... read the arguments
+               for (i=0; arg[i]; i+=2) 
+               {
+                       if(!strcmp(arg[i], "inst"))
+                               id = 0;
+
+                       else if(!strcmp(arg[i], "update"))
+                               id = 1;
+
+                       else if(!strcmp(arg[i], "uinst"))
+                               id = 2;
+
+                       else if(!strcmp(arg[i], "srcinst"))
+                               id = 3;
+
+                       else
+                               continue;
+
+                       pack->files[id] = new char [strlen(arg[i+1])+1];
+                       strcpy(pack->files[id], arg[i+1]);
+               }
+       }
+
+       // ... save the field
+       else
+       {
+               if(!strcmp(tag, "name"))
+                       pack->field = &pack->name;
+
+               else if(!strcmp(tag, "description"))
+                       pack->field = &pack->description;
+
+               else if (!strcmp(tag, "depent"))
+               {
+                       pack->depencies.push_back((char*)NULL);
+                       pack->field = &pack->depencies.back();
+               }
+       }
+}
+
+// expat callback for end of a package tag
+void pack_end (void* usrdata, const char* tag)
+{
+       PACKAGE* pack = (PACKAGE*)usrdata;
+
+       pack->field = NULL;
+}
+
+// expat callback for text
+void pack_text (void* usrdata, const char* data, int len)
+{
+       PACKAGE* pack = (PACKAGE*)usrdata;
+
+       if(!pack->field)
+               return;
+
+       *pack->field = new char[len+1];
+       strncpy(*pack->field, data, len);
+       (*pack->field)[len] = '\0';
+}
+
+// The user clicks on a package
+extern "C" int PML_LoadPackage (TREE* tree, int id, PML_SetButton SetButton)
+{
+       PACKAGE* pack = &tree->packages[id];
+       tree->setButton = SetButton;
+
+       if(SetButton)
+       {
+               SetButton(1, pack->action);
+               SetButton(2, pack->inst); // && pack->action != 0
+               SetButton(3, pack->src_inst); 
+               SetButton(4, pack->update);
+               SetButton(5, pack->uninstall); 
+       }
+
+       // root notes (like network) return here
+       if(!pack->path)
+               return 1;
+
+       if(!pack->loaded)
+       {
+               PML_XmlDownload (tree, pack->path, (void*)pack, pack_start, pack_end, pack_text);
+               pack->loaded = TRUE;
+       }
+
+       return ERR_OK;
+}
+
+extern "C" int PML_FindItem (TREE* tree, const char* what)
+{
+       int i, j;
+       bool found;
+
+       // if we have children, same action for them
+       for (i=1; (UINT)i<tree->packages.size(); i++)
+       {
+               found = true;
+
+               for(j=0; (UINT)j<strlen(what); j++)
+               {
+                       if(tolower(what[j]) != tolower(tree->packages[i].name[j]))
+                       {
+                               found = false;
+                               break;
+                       }
+               }
+
+               if(found)
+                       return i;
+       }
+
+       return 0;
+}
+
+// The user chooses a actions like Install
+extern "C" int PML_SetAction (TREE* tree, int id, int action, PML_SetIcon SetIcon, PML_Ask Ask)
+{
+       UINT i;
+       int ret = ERR_OK;
+
+       tree->setIcon = SetIcon;
+       PACKAGE* pack = &tree->packages[id];
+
+       // if we have children, same action for them
+       for (i=0; i<pack->children.size(); i++)
+               ret = ret || PML_SetAction(tree, pack->children[i], action, SetIcon, Ask);
+
+       // is the action possible ? 
+       if(!pack->actions[action])
+               return ERR_GENERIC;
+
+       // is it already set 
+       if(pack->action == action)
+               return ERR_OK;
+
+       //
+       if(pack->depencies.size() && action)
+       {
+               UINT count = pack->depencies.size();
+               WCHAR buffer[2000], buffer2[200], errbuf[2000];
+               PML_TransError(ERR_DEP1, (WCHAR*)buffer, sizeof(buffer)/sizeof(WCHAR));
+
+               for (i=0; i<pack->depencies.size(); i++)
+               {
+                       int item = PML_FindItem(tree, pack->depencies[i]);
+
+                       if(!item)
+                               return ERR_GENERIC;
+
+                       if(action == tree->packages[item].action)// || tree->packages[item].installed
+                       {
+                               count--;
+                               continue;
+                       }
+
+                       MultiByteToWideChar (CP_ACP, 0, pack->depencies[i], strlen(pack->depencies[i])+1, buffer2, 200);
+                       wsprintf(buffer, L"%s - %s\n", buffer, buffer2);//
+               }
+
+               wcscat(buffer, PML_TransError(ERR_DEP2, (WCHAR*)errbuf, sizeof(errbuf)/sizeof(WCHAR)));
+
+               if(count)
+               {
+                       if(!Ask(buffer))
+                               return ERR_GENERIC;
+
+                       for (i=0; i<pack->depencies.size(); i++)
+                       {
+                               int item = PML_FindItem(tree, pack->depencies[i]);
+
+                               tree->packages[item].neededBy.push_back(id);
+
+                               PML_SetAction(tree, item, action, SetIcon, Ask);
+                       }
+               }
+       }
+
+       // load it if it's not loaded yet
+       else if (!pack->loaded && pack->path)
+       {
+               PML_XmlDownload (tree, pack->path, (void*)pack, pack_start, pack_end, pack_text);
+               pack->loaded = TRUE;
+
+               return PML_SetAction(tree, id, action, SetIcon, Ask);
+       }
+
+       // set the icon
+       if(SetIcon && !pack->icon)
+                       SetIcon(id, action);
+
+       // set the button(s)
+       if(tree->setButton && action != 2)
+       {
+               tree->setButton(1, action);
+               //tree->setButton(pack->action+1, action);
+       }
+
+       // can't do src install yet
+       if(action == 2)
+       {
+               MessageBox(0, L"Sorry, but source install is not implemented yet.", 0,0);
+               return ERR_OK;
+       }
+
+       // everything but undoing is done here
+       else if (action != 0)
+       {
+               // since we are setting a action we undo it again
+               if(tree->setButton)
+                       tree->setButton(1, 1);
+               //tree->setButton(action+1, 0);
+
+               pack->action = action;
+
+               // root notes (like network) return here
+               if(!pack->path)
+                       return ret; 
+
+               // save the name of the corresponding script in a vector
+               tree->todo.push_back(pack->files[action-1]);
+       }
+
+       // undoing
+       else 
+       {
+               for(i=0; i<pack->neededBy.size(); i++)
+               {
+                       if(tree->packages[pack->neededBy[i]].action)
+                       {
+                               SetIcon(id, pack->action);
+                               return ERR_GENERIC;
+                       }
+               }
+
+               // root notes (like network) return here
+               if(!pack->path || pack->action==0)
+                       return ret; 
+
+               // erase from todo list
+               for(i=0; i<tree->todo.size(); i++)
+                       if(!strcmp(tree->todo[i], pack->files[pack->action-1])) // look for right entry
+                               tree->todo.erase(tree->todo.begin()+i); // delete it
+
+               // set action back
+               pack->action = 0;
+       }
+
+       return ret;
+}
+
+//
+extern "C" char* PML_GetDescription (TREE* tree, int id)
+{
+       PML_LoadPackage(tree, id, NULL);
+
+       return tree->packages[id].description;
+}
+