[gnote] Add checking for plug-in compatibility



commit bfabc1a1b60c2d5438d1d58e6658acace51ba54a
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Mar 15 20:29:54 2014 +0200

    Add checking for plug-in compatibility

 configure.ac         |    2 +
 src/addininfo.cpp    |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/addininfo.hpp    |    5 ++++
 src/addinmanager.cpp |    3 ++
 4 files changed, 66 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 4858e6b..c1d7007 100644
--- a/configure.ac
+++ b/configure.ac
@@ -16,8 +16,10 @@ GNOTE_VERSION="$PACKAGE_VERSION"
 AC_SUBST(GNOTE_VERSION)
 LIBGNOTE_RELEASE=${PACKAGE_VERSION%.*}
 AC_SUBST(LIBGNOTE_RELEASE)
+AC_DEFINE_UNQUOTED([LIBGNOTE_RELEASE], ["$LIBGNOTE_RELEASE"], [libgnote release.])
 LIBGNOTE_VERSION_INFO=1:0:0
 AC_SUBST(LIBGNOTE_VERSION_INFO)
+AC_DEFINE_UNQUOTED([LIBGNOTE_VERSION_INFO], ["$LIBGNOTE_VERSION_INFO"], [libgnote release.])
 
 
 dnl all the library version.
diff --git a/src/addininfo.cpp b/src/addininfo.cpp
index bc4da94..33dd48c 100644
--- a/src/addininfo.cpp
+++ b/src/addininfo.cpp
@@ -19,12 +19,15 @@
 
 #include <stdexcept>
 
+#include <boost/lexical_cast.hpp>
+
 #include <glibmm/i18n.h>
 #include <glibmm/keyfile.h>
 
 #include "base/macros.hpp"
 #include "addininfo.hpp"
 #include "debug.hpp"
+#include "sharp/string.hpp"
 
 
 namespace gnote {
@@ -89,6 +92,8 @@ void AddinInfo::load_from_file(const std::string & info_file)
       DBG_OUT("Can't read default enabled status, assuming default: %s", e.what().c_str());
     }
     m_addin_module = addin_info.get_string(ADDIN_INFO, "Module");
+    m_libgnote_release = addin_info.get_string(ADDIN_INFO, "LibgnoteRelease");
+    m_libgnote_version_info = addin_info.get_string(ADDIN_INFO, "LibgnoteVersionInfo");
 
     if(addin_info.has_group(ADDIN_ATTS)) {
       FOREACH(const Glib::ustring & key, addin_info.get_keys(ADDIN_ATTS)) {
@@ -110,5 +115,56 @@ Glib::ustring AddinInfo::get_attribute(const Glib::ustring & att)
   return Glib::ustring();
 }
 
+bool AddinInfo::validate(const Glib::ustring & release, const Glib::ustring & version_info) const
+{
+  if(validate_compatibility(release, version_info)) {
+    return true;
+  }
+
+  ERR_OUT(_("Incompatible plug-in %s: expected %s, got %s"),
+          m_id.c_str(), (release + " " + version_info).c_str(),
+          (m_libgnote_release + " " + m_libgnote_version_info).c_str());
+  return false;
+}
+
+bool AddinInfo::validate_compatibility(const Glib::ustring & release, const Glib::ustring & version_info) 
const
+{
+  if(release != m_libgnote_release) {
+    return false;
+  }
+  if(version_info == m_libgnote_version_info) {
+    return true;
+  }
+  else {
+    try {
+      std::vector<std::string> parts;
+      sharp::string_split(parts, m_libgnote_version_info, ":");
+      if(parts.size() != 3) {
+        return false;
+      }
+
+      int this_ver = boost::lexical_cast<int>(parts[0]);
+      parts.clear();
+      sharp::string_split(parts, version_info, ":");
+      int ver = boost::lexical_cast<int>(parts[0]);
+      int compat = boost::lexical_cast<int>(parts[2]);
+
+      if(this_ver > ver) {
+        // too new
+        return false;
+      }
+      if(this_ver < ver - compat) {
+        // too old
+        return false;
+      }
+
+      return true;
+    }
+    catch(std::bad_cast &) {
+      return false;
+    }
+  }
+}
+
 
 }
diff --git a/src/addininfo.hpp b/src/addininfo.hpp
index 096aed4..40e1342 100644
--- a/src/addininfo.hpp
+++ b/src/addininfo.hpp
@@ -89,6 +89,7 @@ public:
       return m_attributes;
     }
   Glib::ustring get_attribute(const Glib::ustring & att);
+  bool validate(const Glib::ustring & release, const Glib::ustring & version_info) const;
 private:
   std::string m_id;
   std::string m_name;
@@ -99,8 +100,12 @@ private:
   std::string m_copyright;
   bool m_default_enabled;
   std::string m_addin_module;
+  Glib::ustring m_libgnote_release;
+  Glib::ustring m_libgnote_version_info;
 
   std::map<Glib::ustring, Glib::ustring> m_attributes;
+
+  bool validate_compatibility(const Glib::ustring & release, const Glib::ustring & version_info) const;
 };
 
 }
diff --git a/src/addinmanager.cpp b/src/addinmanager.cpp
index 1c4c611..b83391b 100644
--- a/src/addinmanager.cpp
+++ b/src/addinmanager.cpp
@@ -212,6 +212,9 @@ namespace {
     for(std::list<std::string>::iterator iter = files.begin(); iter != files.end(); ++iter) {
       try {
         AddinInfo addin_info(*iter);
+        if(!addin_info.validate(LIBGNOTE_RELEASE, LIBGNOTE_VERSION_INFO)) {
+          continue;
+        }
         std::string module = Glib::build_filename(path, addin_info.addin_module());
         if(sharp::file_exists(module + "." + G_MODULE_SUFFIX)) {
           addin_info.addin_module(module);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]