--amend
[supertux.git] / src / addon / addon_manager.cpp
index 68a50c5..fe2de83 100644 (file)
@@ -1,12 +1,10 @@
-//  $Id$
-//
 //  SuperTux - Add-on Manager
 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
 //
-//  This program is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU General Public License
-//  as published by the Free Software Foundation; either version 2
-//  of the License, or (at your option) any later version.
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
 //
 //  This program is distributed in the hope that it will be useful,
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  GNU General Public License for more details.
 //
 //  You should have received a copy of the GNU General Public License
-//  along with this program; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-//  02111-1307, USA.
-//
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "addon/addon_manager.hpp"
 
+#include <config.h>
+#include <version.h>
+
+#include <algorithm>
+#include <memory>
+#include <physfs.h>
 #include <sstream>
 #include <stdexcept>
-#include <cstdlib>
-#include <list>
-#include <physfs.h>
 #include <sys/stat.h>
-#include <stdio.h>
-#include "addon/addon_manager.hpp"
-#include "config.h"
-#include "log.hpp"
-#include "lisp/parser.hpp"
-#include "lisp/lisp.hpp"
-#include "lisp/list_iterator.hpp"
-#include "physfs/physfs_stream.hpp"
 
 #ifdef HAVE_LIBCURL
-#include <curl/curl.h>
-#include <curl/types.h>
-#include <curl/easy.h>
+#  include <curl/curl.h>
+#  include <curl/easy.h>
 #endif
 
+#include "addon/addon.hpp"
+#include "lisp/list_iterator.hpp"
+#include "lisp/parser.hpp"
+#include "util/reader.hpp"
+#include "util/writer.hpp"
+#include "util/log.hpp"
+
 #ifdef HAVE_LIBCURL
 namespace {
 
-  size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
-  {
-    std::string& s = *static_cast<std::string*>(string_ptr);
-    std::string buf(static_cast<char*>(ptr), size * nmemb);
-    s += buf;
-    log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
-    return size * nmemb;
-  }
+size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
+{
+  std::string& s = *static_cast<std::string*>(string_ptr);
+  std::string buf(static_cast<char*>(ptr), size * nmemb);
+  s += buf;
+  log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
+  return size * nmemb;
+}
 
-  size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
-  {
-    PHYSFS_file* f = static_cast<PHYSFS_file*>(f_p);
-    PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
-    log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
-    return size * written;
-  }
+size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
+{
+  PHYSFS_file* f = static_cast<PHYSFS_file*>(f_p);
+  PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
+  log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
+  return size * written;
+}
 
 }
 #endif
@@ -70,7 +68,9 @@ AddonManager::get_instance()
   return instance;
 }
 
-AddonManager::AddonManager()
+AddonManager::AddonManager() :
+  addons(),
+  ignored_addon_filenames()
 {
 #ifdef HAVE_LIBCURL
   curl_global_init(CURL_GLOBAL_ALL);
@@ -89,12 +89,12 @@ AddonManager::~AddonManager()
 std::vector<Addon*>
 AddonManager::get_addons()
 {
-/*
-  for (std::vector<Addon>::iterator it = installed_addons.begin(); it != installed_addons.end(); ++it) {
+  /*
+    for (std::vector<Addon>::iterator it = installed_addons.begin(); it != installed_addons.end(); ++it) {
     Addon& addon = *it;
     if (addon.md5 == "") addon.md5 = calculate_md5(addon);
-  }
-*/
+    }
+  */
   return addons;
 }
 
@@ -104,7 +104,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 +135,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::auto_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 
       bool exists = false;
       for (std::vector<Addon*>::const_iterator i = addons.begin(); i != addons.end(); i++) {
-        if (**i == addon) {
+        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;
@@ -178,7 +180,6 @@ AddonManager::check_online()
 #endif
 }
 
-
 void
 AddonManager::install(Addon* addon)
 {
@@ -386,10 +387,10 @@ AddonManager::load_addons()
       // make sure it looks like an infoFile
       static const std::string infoExt = ".nfo";
       if (potentialInfoFileName.length() <= infoExt.length())
-         continue;
+        continue;
 
       if (potentialInfoFileName.compare(potentialInfoFileName.length()-infoExt.length(), infoExt.length(), infoExt) != 0)
-         continue;
+        continue;
 
       // make sure it's in the current archive
       std::string infoFileDir = PHYSFS_getRealDir(potentialInfoFileName.c_str());
@@ -427,16 +428,16 @@ AddonManager::load_addons()
   PHYSFS_freeList(rc);
 }
 
-
 void
-AddonManager::read_config(const lisp::Lisp& lisp)
+AddonManager::read(const Reader& lisp)
 {
-  lisp.get_vector("disabled-addons", ignored_addon_filenames); 
+  lisp.get("disabled-addons", ignored_addon_filenames); 
 }
 
 void
-AddonManager::write_config(lisp::Writer& writer)
+AddonManager::write(lisp::Writer& writer)
 {
-  writer.write_string_vector("disabled-addons", ignored_addon_filenames); 
+  writer.write("disabled-addons", ignored_addon_filenames); 
 }
 
+/* EOF */