Author: lslezak
Date: Fri Aug 22 13:40:27 2008
New Revision: 50328
URL: http://svn.opensuse.org/viewcvs/yast?rev=50328&view=rev
Log:
- added ServiceManager and PkgService classes for service support in
pkg-bindings
Added:
trunk/pkg-bindings/src/PkgService.cc
trunk/pkg-bindings/src/PkgService.h
trunk/pkg-bindings/src/ServiceManager.cc
trunk/pkg-bindings/src/ServiceManager.h
Added: trunk/pkg-bindings/src/PkgService.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/pkg-bindings/src/PkgService.cc?rev=50328&view=auto
==============================================================================
--- trunk/pkg-bindings/src/PkgService.cc (added)
+++ trunk/pkg-bindings/src/PkgService.cc Fri Aug 22 13:40:27 2008
@@ -0,0 +1,56 @@
+/* ------------------------------------------------------------------------------
+ * Copyright (c) 2008 Novell, Inc. All Rights Reserved.
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of version 2 of the GNU General Public License as published by the
+ * Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the 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, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may find
+ * current contact information at www.novell.com.
+ * ------------------------------------------------------------------------------
+ */
+
+
+#include "PkgService.h"
+
+PkgService::PkgService() : _deleted(false), _old_alias()
+{
+}
+
+PkgService::PkgService(const zypp::ServiceInfo &s, const std::string &old_alias) :
+ zypp::ServiceInfo(s), _deleted(false), _old_alias(old_alias.empty() ? s.alias() : old_alias)
+{
+}
+
+PkgService::PkgService(const PkgService &s) :
+ zypp::ServiceInfo(s), _deleted(s._deleted), _old_alias(s._old_alias)
+{
+}
+
+PkgService::~PkgService()
+{
+}
+
+bool PkgService::isDeleted() const
+{
+ return _deleted;
+}
+
+void PkgService::setDeleted()
+{
+ _deleted = true;
+}
+
+std::string PkgService::origAlias() const
+{
+ return _old_alias;
+}
+
Added: trunk/pkg-bindings/src/PkgService.h
URL: http://svn.opensuse.org/viewcvs/yast/trunk/pkg-bindings/src/PkgService.h?rev=50328&view=auto
==============================================================================
--- trunk/pkg-bindings/src/PkgService.h (added)
+++ trunk/pkg-bindings/src/PkgService.h Fri Aug 22 13:40:27 2008
@@ -0,0 +1,50 @@
+/* ------------------------------------------------------------------------------
+ * Copyright (c) 2008 Novell, Inc. All Rights Reserved.
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of version 2 of the GNU General Public License as published by the
+ * Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the 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, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may find
+ * current contact information at www.novell.com.
+ * ------------------------------------------------------------------------------
+ */
+
+#include <string>
+#include
+
+// Extend zypp::ServiceInfo class to contain "deleted" flag and original service alias
+class PkgService : public zypp::ServiceInfo
+{
+ public:
+
+ PkgService();
+
+ PkgService(const zypp::ServiceInfo &s, const std::string &old_alias = "");
+
+ PkgService(const PkgService &s);
+
+ ~PkgService();
+
+ bool isDeleted() const;
+
+ void setDeleted();
+
+ std::string origAlias() const;
+
+
+ private:
+
+ bool _deleted;
+ std::string _old_alias;
+};
+
+
Added: trunk/pkg-bindings/src/ServiceManager.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/pkg-bindings/src/ServiceManager.cc?rev=50328&view=auto
==============================================================================
--- trunk/pkg-bindings/src/ServiceManager.cc (added)
+++ trunk/pkg-bindings/src/ServiceManager.cc Fri Aug 22 13:40:27 2008
@@ -0,0 +1,294 @@
+/* ------------------------------------------------------------------------------
+ * Copyright (c) 2008 Novell, Inc. All Rights Reserved.
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of version 2 of the GNU General Public License as published by the
+ * Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the 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, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may find
+ * current contact information at www.novell.com.
+ * ------------------------------------------------------------------------------
+ */
+
+
+#include "log.h"
+#include "ServiceManager.h"
+
+#include
+
+ServiceManager::ServiceManager() : _services_loaded(false)
+{
+}
+
+ServiceManager::~ServiceManager()
+{
+}
+
+bool ServiceManager::LoadServices(const zypp::RepoManager &repomgr)
+{
+ bool ret = true;
+
+ if (!_services_loaded)
+ {
+ try
+ {
+ for_ (it, repomgr.serviceBegin(), repomgr.serviceEnd())
+ {
+ PkgService s(*it);
+ y2milestone("Loaded service %s (%s)", s.alias().c_str(), s.url().asString().c_str());
+ //_known_services.insert(std::pair);
+ }
+
+ _services_loaded = true;
+ }
+ catch(...)
+ {
+ ret = false;
+ }
+ }
+ else
+ {
+ y2warning("Services have already been loaded, skipping load");
+ }
+
+ return ret;
+}
+
+bool ServiceManager::SaveServices(zypp::RepoManager &repomgr) const
+{
+ bool ret = true;
+
+ for_ (it, _known_services.begin(), _known_services.end())
+ {
+ std::string alias(it->second.alias());
+
+ try
+ {
+ // save or delete
+ if (it->second.isDeleted())
+ {
+ y2milestone("Removing service %s", alias.c_str());
+ repomgr.removeService(alias);
+ }
+ }
+ catch(...)
+ {
+ y2error("Cannot delete service %s", alias.c_str());
+ ret = false;
+ }
+ }
+
+ for_ (it, _known_services.begin(), _known_services.end())
+ {
+ std::string alias(it->second.alias());
+
+ try
+ {
+ // save
+ if (!it->second.isDeleted())
+ {
+ if (it->second != repomgr.getService(alias))
+ {
+ y2milestone("Saving modified service %s", alias.c_str());
+ // FIXME use old alias
+ repomgr.modifyService(alias, it->second);
+ }
+ }
+ }
+ catch(...)
+ {
+ y2error("Cannot service %s", alias.c_str());
+ ret = false;
+ }
+ }
+
+ return ret;
+}
+
+bool ServiceManager::RefreshService(const std::string &alias, zypp::RepoManager &repomgr) const
+{
+ try
+ {
+ repomgr.refreshService(alias);
+ return true;
+ }
+ catch(...)
+ {
+ return false;
+ }
+}
+
+ServiceManager::Services ServiceManager::GetServices() const
+{
+ Services ret;
+
+ for_ (it, _known_services.begin(), _known_services.end())
+ {
+ // return only valid services
+ if (!it->second.isDeleted())
+ {
+ ret.push_back(it->second);
+ }
+ }
+
+ return ret;
+}
+
+bool ServiceManager::AddService(const std::string &alias, const std::string &url)
+{
+ try
+ {
+ if (alias.empty())
+ {
+ y2error("Empty alias for service %s", url.c_str());
+ return false;
+ }
+
+ PkgServices::iterator serv_it = _known_services.find(alias);
+
+ if (serv_it != _known_services.end())
+ {
+ if (serv_it->second.isDeleted())
+ {
+ // we are adding an already removed service,
+ // remove the existing service
+ _known_services.erase(serv_it);
+ }
+ else
+ {
+ y2error("Service with alias %s already exists", alias.c_str());
+ return false;
+ }
+ }
+
+ PkgService srv;
+ srv.setAlias(alias);
+ srv.setUrl(url);
+
+ // FIXME _known_services.insert(std::pair);
+ }
+ catch(...)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+bool ServiceManager::RemoveService(const std::string &alias)
+{
+ try
+ {
+ PkgServices::iterator serv_it = _known_services.find(alias);
+
+ if (serv_it != _known_services.end())
+ {
+ if (serv_it->second.isDeleted())
+ {
+ y2warning("Service %s has been already removed", alias.c_str());
+ return true;
+ }
+ else
+ {
+ serv_it->second.setDeleted();
+ y2milestone("Service %s has been marked as deleted", alias.c_str());
+ return true;
+ }
+ }
+ else
+ {
+ y2error("Service %s does not exist", alias.c_str());
+ return false;
+ }
+ }
+ catch(...)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+void ServiceManager::Reset()
+{
+ y2milestone("Resetting known services...");
+ _known_services.clear();
+}
+
+
+zypp::ServiceInfo ServiceManager::GetService(const std::string &alias) const
+{
+ try
+ {
+ PkgServices::const_iterator serv_it = _known_services.find(alias);
+
+ if (serv_it != _known_services.end())
+ {
+ if (serv_it->second.isDeleted())
+ {
+ y2warning("Service %s has been removed", alias.c_str());
+ return zypp::ServiceInfo::noService;
+ }
+ else
+ {
+ return serv_it->second;
+ }
+ }
+ else
+ {
+ y2error("Service %s does not exist", alias.c_str());
+ return zypp::ServiceInfo::noService;
+ }
+ }
+ catch(...)
+ {
+ }
+
+ return zypp::ServiceInfo::noService;
+}
+
+// FIXME allow to change the alias
+bool ServiceManager::SetService(const std::string old_alias, const zypp::ServiceInfo &srv)
+{
+ try
+ {
+ std::string alias(srv.alias());
+
+ PkgServices::const_iterator serv_it = _known_services.find(alias);
+
+ if (serv_it != _known_services.end())
+ {
+ if (serv_it->second.isDeleted())
+ {
+ y2warning("Service %s has been removed", alias.c_str());
+ return false;
+ }
+ else
+ {
+ PkgService s(srv);
+ _known_services[alias] = s;
+ return true;
+ }
+ }
+ else
+ {
+ y2error("Service %s does not exist", alias.c_str());
+ return false;
+ }
+ }
+ catch(...)
+ {
+ }
+
+ return false;
+}
+
+
Added: trunk/pkg-bindings/src/ServiceManager.h
URL: http://svn.opensuse.org/viewcvs/yast/trunk/pkg-bindings/src/ServiceManager.h?rev=50328&view=auto
==============================================================================
--- trunk/pkg-bindings/src/ServiceManager.h (added)
+++ trunk/pkg-bindings/src/ServiceManager.h Fri Aug 22 13:40:27 2008
@@ -0,0 +1,70 @@
+/* ------------------------------------------------------------------------------
+ * Copyright (c) 2008 Novell, Inc. All Rights Reserved.
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of version 2 of the GNU General Public License as published by the
+ * Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the 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, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may find
+ * current contact information at www.novell.com.
+ * ------------------------------------------------------------------------------
+ */
+
+
+#include <map>
+#include <string>
+#include "PkgService.h"
+
+namespace zypp
+{
+ class RepoManager;
+}
+
+class ServiceManager
+{
+ public:
+
+ typedef std::listzypp::ServiceInfo Services;
+
+ ServiceManager();
+
+ ~ServiceManager();
+
+ Services GetServices() const;
+
+ bool LoadServices(const zypp::RepoManager &repomgr);
+
+ bool SaveServices(zypp::RepoManager &repomgr) const;
+
+ bool AddService(const std::string &alias, const std::string &url);
+
+ bool RemoveService(const std::string &alias);
+
+ zypp::ServiceInfo GetService(const std::string &alias) const;
+
+ bool SetService(const std::string old_alias, const zypp::ServiceInfo &srv);
+
+ bool RefreshService(const std::string &alias, zypp::RepoManager &repomgr) const;
+
+ void Reset();
+
+
+ private:
+
+ // alias -> PkgService
+ typedef std::map PkgServices;
+
+ bool _services_loaded;
+
+ PkgServices _known_services;
+};
+
+
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org