[glom/feature_backup2] Saving from examples: Create the groups mentioned in the document.



commit f4b6374fb3df5e63c12b9adb15a36eb4c6126950
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Jul 21 10:16:35 2010 +0200

    Saving from examples: Create the groups mentioned in the document.
    
    * glom/base_db.[h|cc]: Added add_groups_from_document().
    * glom/application.cc: recreate_database_from_example() call it.

 ChangeLog           |    7 ++
 glom/application.cc |   19 ++++--
 glom/base_db.cc     |  152 +++++++++++++++++++++++++++++++++++----------------
 glom/base_db.h      |   17 +++---
 4 files changed, 132 insertions(+), 63 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 889e8ac..06ea47a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2010-07-21  Murray Cumming  <murrayc murrayc com>
+
+	Saving from examples: Create the groups mentioned in the document.
+
+	* glom/base_db.[h|cc]: Added add_groups_from_document().
+	* glom/application.cc: recreate_database_from_example() call it.
+
 2010-07-20  Murray Cumming  <murrayc murrayc com>>
 
 	Fixed warnings with latest gtkmm-2.24
diff --git a/glom/application.cc b/glom/application.cc
index 44d98de..a39b038 100644
--- a/glom/application.cc
+++ b/glom/application.cc
@@ -1795,6 +1795,18 @@ bool Application::recreate_database_from_example(bool& user_cancelled)
 
   dialog_progress->pulse();
 
+  //Create the developer group, and make this user a member of it:
+  dialog_progress->pulse();
+  bool test = m_pFrame->add_standard_groups();
+  if(!test)
+    return false;
+
+  //Add any extra groups from the example file:
+  dialog_progress->pulse();
+  test = m_pFrame->add_groups_from_document();
+  if(!test)
+    return false;
+
   //Create each table:
   Document::type_listTableInfo tables = pDocument->get_tables();
   for(Document::type_listTableInfo::const_iterator iter = tables.begin(); iter != tables.end(); ++iter)
@@ -1818,13 +1830,6 @@ bool Application::recreate_database_from_example(bool& user_cancelled)
   dialog_progress->pulse();
   m_pFrame->add_standard_tables(); //Add internal, hidden, tables.
 
-  //Create the developer group, and make this user a member of it:
-  //If we got this far then the user must really have developer privileges already:
-  dialog_progress->pulse();
-  const bool test = m_pFrame->add_standard_groups();
-  if(!test)
-    return false;
-
   for(Document::type_listTableInfo::const_iterator iter = tables.begin(); iter != tables.end(); ++iter)
   {
     sharedptr<const TableInfo> table_info = *iter;
diff --git a/glom/base_db.cc b/glom/base_db.cc
index e51f8e7..f23fc6b 100644
--- a/glom/base_db.cc
+++ b/glom/base_db.cc
@@ -1223,65 +1223,121 @@ bool Base_DB::add_standard_groups()
 #endif
 
   // If the connection doesn't support users we can skip this step
-  if(sharedconnection->get_gda_connection()->supports_feature(Gnome::Gda::CONNECTION_FEATURE_USERS))
+  if(!(sharedconnection->get_gda_connection()->supports_feature(Gnome::Gda::CONNECTION_FEATURE_USERS)))
   {
-    const type_vec_strings vecGroups = Privs::get_database_groups();
-    type_vec_strings::const_iterator iterFind = std::find(vecGroups.begin(), vecGroups.end(), devgroup);
-    if(iterFind == vecGroups.end())
+    std::cout << "DEBUG: Connection does not support users" << std::endl;
+  }
+
+  const type_vec_strings vecGroups = Privs::get_database_groups();
+  type_vec_strings::const_iterator iterFind = std::find(vecGroups.begin(), vecGroups.end(), devgroup);
+  if(iterFind == vecGroups.end())
+  {
+    //The "SUPERUSER" here has no effect because SUPERUSER is not "inherited" to member users.
+    //But let's keep it to make the purpose of this group obvious.
+    bool test = query_execute("CREATE GROUP \"" GLOM_STANDARD_GROUP_NAME_DEVELOPER "\" WITH SUPERUSER");
+    if(!test)
     {
-      //The "SUPERUSER" here has no effect because SUPERUSER is not "inherited" to member users.
-      //But let's keep it to make the purpose of this group obvious.
-      bool test = query_execute("CREATE GROUP \"" GLOM_STANDARD_GROUP_NAME_DEVELOPER "\" WITH SUPERUSER");
-      if(!test)
-      {
-        std::cerr << "Glom Base_DB::add_standard_groups(): CREATE GROUP failed when adding the developer group." << std::endl;
-        return false;
-      }
+      std::cerr << "Glom Base_DB::add_standard_groups(): CREATE GROUP failed when adding the developer group." << std::endl;
+      return false;
+    }
 
-      //Make sure the current user is in the developer group.
-      //(If he is capable of creating these groups then he is obviously a developer, and has developer rights on the postgres server.)
-      const Glib::ustring current_user = ConnectionPool::get_instance()->get_user();
-      Glib::ustring strQuery = "ALTER GROUP \"" GLOM_STANDARD_GROUP_NAME_DEVELOPER "\" ADD USER \"" + current_user + "\"";
-      test = query_execute(strQuery);
-      if(!test)
-      {
-        std::cerr << "Glom Base_DB::add_standard_groups(): ALTER GROUP failed when adding the user to the developer group." << std::endl;
-        return false;
-      }
+    //Make sure the current user is in the developer group.
+    //(If he is capable of creating these groups then he is obviously a developer, and has developer rights on the postgres server.)
+    const Glib::ustring current_user = ConnectionPool::get_instance()->get_user();
+    Glib::ustring strQuery = "ALTER GROUP \"" GLOM_STANDARD_GROUP_NAME_DEVELOPER "\" ADD USER \"" + current_user + "\"";
+    test = query_execute(strQuery);
+    if(!test)
+    {
+      std::cerr << "Glom Base_DB::add_standard_groups(): ALTER GROUP failed when adding the user to the developer group." << std::endl;
+      return false;
+    }
 
-      std::cout << "DEBUG: Added user " << current_user << " to glom developer group on postgres server." << std::endl;
+    std::cout << "DEBUG: Added user " << current_user << " to glom developer group on postgres server." << std::endl;
 
-      Privileges priv_devs;
-      priv_devs.m_view = true;
-      priv_devs.m_edit = true;
-      priv_devs.m_create = true;
-      priv_devs.m_delete = true;
+    Privileges priv_devs;
+    priv_devs.m_view = true;
+    priv_devs.m_edit = true;
+    priv_devs.m_create = true;
+    priv_devs.m_delete = true;
 
-      Document::type_listTableInfo table_list = get_document()->get_tables(true /* including system prefs */);
+    Document::type_listTableInfo table_list = get_document()->get_tables(true /* including system prefs */);
 
-      for(Document::type_listTableInfo::const_iterator iter = table_list.begin(); iter != table_list.end(); ++iter)
-      {
-        sharedptr<const TableInfo> table_info = *iter;
-        if(table_info)
-        {
-          const Glib::ustring table_name = table_info->get_name();
-          if(get_table_exists_in_database(table_name)) //Maybe the table has not been created yet.
-            Privs::set_table_privileges(devgroup, table_name, priv_devs, true /* developer privileges */);
-        }
-      }
+    for(Document::type_listTableInfo::const_iterator iter = table_list.begin(); iter != table_list.end(); ++iter)
+    {
+      sharedptr<const TableInfo> table_info = *iter;
+      if(!table_info)
+        continue;
 
-      //Make sure that it is in the database too:
-      GroupInfo group_info;
-      group_info.set_name(GLOM_STANDARD_GROUP_NAME_DEVELOPER);
-      group_info.m_developer = true;
-      get_document()->set_group(group_info);
+      const Glib::ustring table_name = table_info->get_name();
+      if(get_table_exists_in_database(table_name)) //Maybe the table has not been created yet.
+        Privs::set_table_privileges(devgroup, table_name, priv_devs, true /* developer privileges */);
     }
+
+    //Make sure that it is in the database too:
+    GroupInfo group_info;
+    group_info.set_name(GLOM_STANDARD_GROUP_NAME_DEVELOPER);
+    group_info.m_developer = true;
+    get_document()->set_group(group_info);
   }
-  else
+
+  return true;
+}
+
+bool Base_DB::add_groups_from_document()
+{
+#ifdef GLIBMM_EXCEPTIONS_ENABLED
+  sharedptr<SharedConnection> sharedconnection = connect_to_server();
+#else
+  std::auto_ptr<ExceptionConnection> error;
+  sharedptr<SharedConnection> sharedconnection = connect_to_server(0, error);
+  if(error.get())
+  {
+    g_warning("Base_DB::add_standard_groups: Failed to connect: %s", error->what());
+    // TODO: Rethrow?
+  }
+#endif
+
+  // If the connection doesn't support users we can skip this step
+  if(!(sharedconnection->get_gda_connection()->supports_feature(Gnome::Gda::CONNECTION_FEATURE_USERS)))
   {
     std::cout << "DEBUG: Connection does not support users" << std::endl;
   }
 
+
+  //Get the list of groups from the database server:
+  const type_vec_strings database_groups = Privs::get_database_groups();
+
+  //Get the list of groups from the document:
+  Document* document = get_document();
+  const Document::type_list_groups document_groups = document->get_groups();
+
+  //Add each group if it doesn't exist yet:
+  for(Document::type_list_groups::const_iterator iter = document_groups.begin();
+    iter != document_groups.end(); ++iter)
+  {
+    const GroupInfo& group = *iter;
+    const Glib::ustring name = group.get_name();
+
+    //See if the group exists in the document:
+    type_vec_strings::const_iterator iterFind = std::find(database_groups.begin(), database_groups.end(), name);
+    if(!name.empty() && iterFind == database_groups.end())
+    {
+      Glib::ustring query = "CREATE GROUP \"" + name  + "\"";
+
+      //The "SUPERUSER" here has no effect because SUPERUSER is not "inherited" to member users.
+      //But let's keep it to make the purpose of this group obvious.
+      if(group.m_developer)
+        query += " WITH SUPERUSER";
+
+      const bool test = query_execute(query);
+      if(!test)
+      {
+        std::cerr << G_STRFUNC << ": CREATE GROUP failed when adding the group with name=" << name << std::endl;
+        return false;
+      }
+    }
+  }
+
   return true;
 }
 
@@ -1927,7 +1983,7 @@ sharedptr<LayoutItem_Text> Base_DB::offer_textobject(const sharedptr<LayoutItem_
 
   Dialog_TextObject* dialog = 0;
   Utils::get_glade_widget_derived_with_warning(dialog);
-  
+
   if(transient_for)
     dialog->set_transient_for(*transient_for);
 
@@ -1951,7 +2007,7 @@ sharedptr<LayoutItem_Image> Base_DB::offer_imageobject(const sharedptr<LayoutIte
 
   Dialog_ImageObject* dialog = 0;
   Utils::get_glade_widget_derived_with_warning(dialog);
- 
+
   if(transient_for)
     dialog->set_transient_for(*transient_for);
 
@@ -1975,7 +2031,7 @@ sharedptr<LayoutItem_Notebook> Base_DB::offer_notebook(const sharedptr<LayoutIte
 
   Dialog_Notebook* dialog = 0;
   Utils::get_glade_widget_derived_with_warning(dialog);
-  
+
   if(transient_for)
     dialog->set_transient_for(*transient_for);
 
diff --git a/glom/base_db.h b/glom/base_db.h
index 6fce311..2850cbc 100644
--- a/glom/base_db.h
+++ b/glom/base_db.h
@@ -81,7 +81,7 @@ public:
   /** Execute a SQL Select command, returning the result.
    * This method handles any Gda exceptions caused by executing the command.
    */
-  static Glib::RefPtr<Gnome::Gda::DataModel> query_execute_select(const Glib::ustring& strQuery, 
+  static Glib::RefPtr<Gnome::Gda::DataModel> query_execute_select(const Glib::ustring& strQuery,
                                                                   const Glib::RefPtr<Gnome::Gda::Set>& params = Glib::RefPtr<Gnome::Gda::Set>(0));
 
 
@@ -95,6 +95,7 @@ public:
 
 #ifndef GLOM_ENABLE_CLIENT_ONLY
   bool add_standard_groups();
+  bool add_groups_from_document();
   bool add_standard_tables() const;
 
   bool create_table(const sharedptr<const TableInfo>& table_info, const Document::type_vec_fields& fields) const;
@@ -133,17 +134,17 @@ protected:
   /** Allow the user to select a field from the list of fields for the table.
    */
   sharedptr<LayoutItem_Field> offer_field_list_select_one_field(const Glib::ustring& table_name, Gtk::Window* transient_for = 0);
-  
-  /** Allow the user to select a field from the list of fields for the table, 
+
+  /** Allow the user to select a field from the list of fields for the table,
    * with @a start_field selected by default.
    */
   sharedptr<LayoutItem_Field> offer_field_list_select_one_field(const sharedptr<const LayoutItem_Field>& start_field, const Glib::ustring& table_name, Gtk::Window* transient_for = 0);
-  
-  
+
+
   /** Allow the user to select fields from the list of fields for the table.
    */
   type_list_field_items offer_field_list(const Glib::ustring& table_name, Gtk::Window* transient_for = 0);
-  
+
 
   sharedptr<LayoutItem_Field> offer_field_formatting(const sharedptr<const LayoutItem_Field>& start_field, const Glib::ustring& table_name, Gtk::Window* transient_for = 0);
 
@@ -388,13 +389,13 @@ protected:
    */
   void set_found_set_where_clause_for_portal(FoundSet& found_set, const sharedptr<LayoutItem_Portal>& portal, const Gnome::Gda::Value& foreign_key_value);
 
-  /** Update GDA's information about the table structure, such as the 
+  /** Update GDA's information about the table structure, such as the
    * field list and their types.
    * Call this whenever changing the table structure, for instance with an ALTER query.
    * This may take a few seconds to return.
    */
   void update_gda_metastore_for_table(const Glib::ustring& table_name) const;
-  
+
   static Glib::RefPtr<Gnome::Gda::Connection> get_connection();
 
   static bool get_field_primary_key_index_for_fields(const type_vec_fields& fields, guint& field_column);



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