Turned AddonManager into a Currenton
[supertux.git] / src / addon / addon_manager.cpp
index 4b54a2d..70877b5 100644 (file)
@@ -20,6 +20,7 @@
 #include <version.h>
 
 #include <algorithm>
+#include <memory>
 #include <physfs.h>
 #include <sstream>
 #include <stdexcept>
@@ -28,7 +29,6 @@
 #ifdef HAVE_LIBCURL
 #  include <curl/curl.h>
 #  include <curl/easy.h>
-#  include <curl/types.h>
 #endif
 
 #include "addon/addon.hpp"
@@ -61,13 +61,6 @@ size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
 }
 #endif
 
-AddonManager&
-AddonManager::get_instance()
-{
-  static AddonManager instance;
-  return instance;
-}
-
 AddonManager::AddonManager() :
   addons(),
   ignored_addon_filenames()
@@ -104,7 +97,7 @@ AddonManager::check_online()
 #ifdef HAVE_LIBCURL
   char error_buffer[CURL_ERROR_SIZE+1];
 
-  const char* baseUrl = "http://supertux.berlios.de/addons/index.nfo";
+  const char* baseUrl = "http://addons.supertux.googlecode.com/git/index-0_3_5.nfo";
   std::string addoninfos = "";
 
   CURL *curl_handle;
@@ -135,39 +128,41 @@ AddonManager::check_online()
     if(!addons_lisp) throw std::runtime_error("Downloaded file is not an Add-on list");
 
     lisp::ListIterator iter(addons_lisp);
-    while(iter.next()) {
+    while(iter.next())
+    {
       const std::string& token = iter.item();
-      if(token != "supertux-addoninfo") {
+      if(token != "supertux-addoninfo")
+      {
         log_warning << "Unknown token '" << token << "' in Add-on list" << std::endl;
         continue;
       }
-      Addon* addon_ptr = new Addon();
-      Addon& addon = *addon_ptr;
-      addon.parse(*(iter.lisp()));
-      addon.installed = false;
-      addon.loaded = false;
+      std::unique_ptr<Addon> addon(new Addon());
+      addon->parse(*(iter.lisp()));
+      addon->installed = false;
+      addon->loaded = false;
 
-      // make sure the list of known Add-ons does not already contain this one 
+      // make sure the list of known Add-ons does not already contain this one
       bool exists = false;
       for (std::vector<Addon*>::const_iterator i = addons.begin(); i != addons.end(); i++) {
-        if (**i == addon) {
-          exists = true; 
-          break; 
+        if (**i == *addon) {
+          exists = true;
+          break;
         }
-      } 
-      if (exists) {
-        delete addon_ptr;
-        continue;
       }
 
-      // make sure the Add-on's file name does not contain weird characters
-      if (addon.suggested_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
-        log_warning << "Add-on \"" << addon.title << "\" contains unsafe file name. Skipping." << std::endl;
-        delete addon_ptr;
-        continue;
+      if (exists)
+      {
+        // do nothing
+      }
+      else if (addon->suggested_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos)
+      {
+        // make sure the Add-on's file name does not contain weird characters
+        log_warning << "Add-on \"" << addon->title << "\" contains unsafe file name. Skipping." << std::endl;
+      }
+      else
+      {
+        addons.push_back(addon.release());
       }
-
-      addons.push_back(addon_ptr);
     }
   } catch(std::exception& e) {
     std::stringstream msg;
@@ -242,7 +237,7 @@ AddonManager::install(Addon* addon)
   if (addon->get_md5() != addon->stored_md5) {
     addon->installed = false;
     PHYSFS_delete(fileName.c_str());
-    std::string why = "MD5 checksums differ"; 
+    std::string why = "MD5 checksums differ";
     throw std::runtime_error("Downloading Add-on failed: " + why);
   }
 
@@ -377,10 +372,10 @@ AddonManager::load_addons()
     // Search for infoFiles
     std::string infoFileName = "";
     char** rc2 = PHYSFS_enumerateFiles("/");
-    for(char** i = rc2; *i != 0; ++i) {
+    for(char** j = rc2; *j != 0; ++j) {
 
       // get filename of potential infoFile
-      std::string potentialInfoFileName = *i;
+      std::string potentialInfoFileName = *j;
 
       // make sure it looks like an infoFile
       static const std::string infoExt = ".nfo";
@@ -411,7 +406,7 @@ AddonManager::load_addons()
         addon->loaded = true;
         addons.push_back(addon);
 
-        // check if the Addon is disabled 
+        // check if the Addon is disabled
         if (std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName) != ignored_addon_filenames.end()) {
           unload(addon);
         }
@@ -429,13 +424,13 @@ AddonManager::load_addons()
 void
 AddonManager::read(const Reader& lisp)
 {
-  lisp.get("disabled-addons", ignored_addon_filenames); 
+  lisp.get("disabled-addons", ignored_addon_filenames);
 }
 
 void
 AddonManager::write(lisp::Writer& writer)
 {
-  writer.write("disabled-addons", ignored_addon_filenames); 
+  writer.write("disabled-addons", ignored_addon_filenames);
 }
 
 /* EOF */