This is the mail archive of the cygwin-apps mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH setup 06/13] Add methods for listing possible actions on, and applying one to, a package


The 'action id' is a value of the _actions enum, if positive, otherwise it's
absolute value is a 0-based index into the 'versions' collection to identify
a specific version to install.

v2:
Fix select_action() to consider deftrust

v3:
Fix select_action() for keep
---
 ActionList.h    | 55 +++++++++++++++++++++++++++++++++++++++
 Makefile.am     |  1 +
 PickView.h      |  2 +-
 package_meta.cc | 69 +++++++++++++++++++++++++++++++++++++++++++++++++
 package_meta.h  |  5 +++-
 5 files changed, 130 insertions(+), 2 deletions(-)
 create mode 100644 ActionList.h

diff --git a/ActionList.h b/ActionList.h
new file mode 100644
index 0000000..2e2d424
--- /dev/null
+++ b/ActionList.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 Jon Turney
+ *
+ *     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.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ */
+
+#ifndef SETUP_ACTIONLIST_H
+#define SETUP_ACTIONLIST_H
+
+#include <string>
+#include <vector>
+
+// ---------------------------------------------------------------------------
+// interface to class ActionList
+//
+// a list of Actions possible on a package
+// ---------------------------------------------------------------------------
+
+class Action
+{
+ public:
+  Action(const std::string &_name, int _id, bool _selected, bool _enabled) :
+    name(_name),
+    id(_id),
+    selected(_selected),
+    enabled(_enabled)
+  { };
+
+  std::string name;
+  int id;
+  bool selected;
+  bool enabled;
+};
+
+typedef std::vector<Action> Actions;
+
+class ActionList
+{
+ public:
+  void add(const std::string &name, int id, bool selected, bool enabled)
+  {
+    Action act(name, id, selected, enabled);
+    list.push_back(act);
+  };
+  Actions list;
+};
+
+#endif /* SETUP_ACTIONLIST_H */
diff --git a/Makefile.am b/Makefile.am
index bce4c8c..b58c9b7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -95,6 +95,7 @@ inilint_SOURCES = \
 	-lshlwapi -lcomctl32 -lole32 -lpsapi -luuid -lntdll -lwininet -lws2_32 -lmingw32
 @SETUP@_LDFLAGS = -mwindows -Wc,-static -static-libtool-libs
 @SETUP@_SOURCES = \
+	actionlist.h \
 	AntiVirus.cc \
 	AntiVirus.h \
 	archive.cc \
diff --git a/PickView.h b/PickView.h
index 9777d15..fc9216e 100644
--- a/PickView.h
+++ b/PickView.h
@@ -152,7 +152,7 @@ public:
              pkg != _cat.second.end();
              ++pkg)
           {
-            (*pkg)->set_action(action_id, (*pkg)->trustp(true, deftrust));
+            (*pkg)->select_action(action_id, deftrust);
             l++;
           }
 
diff --git a/package_meta.cc b/package_meta.cc
index dffe3ac..f55c3f3 100644
--- a/package_meta.cc
+++ b/package_meta.cc
@@ -487,6 +487,75 @@ packagemeta::set_action (trusts const trust)
     user_picked = true;
 }
 
+void
+packagemeta::select_action (int id, trusts const deftrust)
+{
+  if (id <= 0)
+    {
+      // Install a specific version
+      set<packageversion>::iterator i = versions.begin ();
+      for (int j = -id; j > 0; j--)
+        i++;
+
+      set_action(Install_action, *i);
+    }
+  else
+    {
+      if (id == packagemeta::Default_action)
+        set_action((packagemeta::_actions)id, installed);
+      else
+        set_action((packagemeta::_actions)id, trustp (true, deftrust));
+    }
+
+  /* Memorize the fact that the user picked at least once. */
+  if (!installed)
+    user_picked = true;
+}
+
+ActionList *
+packagemeta::list_actions(trusts const trust)
+{
+  // first work out the current action, so we can indicate that
+  _actions action;
+
+  if (!desired && installed)
+    action = Uninstall_action;
+  else if (!desired)
+    action = Default_action; // skip
+  else if (desired == installed && picked())
+    action = Reinstall_action;
+  else if (desired == installed)
+    action = Default_action; // keep
+  else
+    action = Install_action;
+
+  // now build the list of possible actions
+  ActionList *al = new ActionList();
+
+  al->add("Uninstall", (int)Uninstall_action, (action == Uninstall_action), bool(installed));
+  al->add("Skip", (int)Default_action, (action == Default_action) && !installed, !installed);
+
+  set<packageversion>::iterator i;
+  for (i = versions.begin (); i != versions.end (); ++i)
+    {
+      if (*i == installed)
+        {
+          al->add("Keep", (int)Default_action, (action == Default_action), TRUE);
+          al->add(packagedb::task == PackageDB_Install ? "Reinstall" : "Retrieve",
+                  (int)Reinstall_action, (action == Reinstall_action), TRUE);
+        }
+      else
+        {
+          al->add(i->Canonical_version().c_str(),
+                  -std::distance(versions.begin (), i),
+                  (action == Install_action) && (*i == desired),
+                  TRUE);
+        }
+    }
+
+  return al;
+}
+
 // Set a particular type of action.
 void
 packagemeta::set_action (_actions action, packageversion const &default_version)
diff --git a/package_meta.h b/package_meta.h
index df66bda..0f01837 100644
--- a/package_meta.h
+++ b/package_meta.h
@@ -26,6 +26,7 @@ class packagemeta;
 #include "package_version.h"
 #include "package_message.h"
 #include "script.h"
+#include "ActionList.h"
 
 typedef std::pair<const std::string, std::vector<packagemeta *> > Category;
 
@@ -52,7 +53,7 @@ public:
 
   enum _actions
     {
-     Default_action,
+     Default_action = 1,
      Install_action,
      Reinstall_action,
      Uninstall_action,
@@ -61,6 +62,8 @@ public:
 
   void set_action (trusts const t);
   void set_action (_actions, packageversion const & default_version);
+  ActionList *list_actions(trusts const trust);
+  void select_action (int id, trusts const deftrust);
 
   void set_message (const std::string& message_id, const std::string& message_string)
   {
-- 
2.17.0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]