diff --git a/data/ui/meson.build b/data/ui/meson.build index 0759598..ed925dc 100644 --- a/data/ui/meson.build +++ b/data/ui/meson.build @@ -11,6 +11,12 @@ tour_gresource = gnome.compile_resources('tour_gresource', c_name: meson.project_name() + '_resource_tour', export: true, install_header: true) +place_gresource = gnome.compile_resources('place_gresource', + 'place.gresource.xml', + source_dir: '.', + c_name: meson.project_name() + '_resource_place', + export: true, + install_header: true) query_gresource = gnome.compile_resources('query_gresource', 'query.gresource.xml', source_dir: '.', diff --git a/data/ui/place.editor.ui b/data/ui/place.editor.ui new file mode 100644 index 0000000..e95bd02 --- /dev/null +++ b/data/ui/place.editor.ui @@ -0,0 +1,296 @@ + + + + + + 5 + 1 + 10 + + + diff --git a/data/ui/place.gresource.xml b/data/ui/place.gresource.xml new file mode 100644 index 0000000..6bef9af --- /dev/null +++ b/data/ui/place.gresource.xml @@ -0,0 +1,19 @@ + + + + + place.editor.ui + + diff --git a/data/ui/tour.places.ui b/data/ui/tour.places.ui index d390332..3782953 100644 --- a/data/ui/tour.places.ui +++ b/data/ui/tour.places.ui @@ -244,6 +244,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND True True Create a new place to associate. + 1 diff --git a/lib/db/lugar.vala b/lib/db/lugar.vala index 38219d4..7276a7b 100644 --- a/lib/db/lugar.vala +++ b/lib/db/lugar.vala @@ -62,16 +62,10 @@ namespace LibSernatur { * Get all tuples and fields from database * @param conn The database connection to use * @return Returns a list of Lugar + * @throws PostgresError If there is a problem with with escaping strings */ - public static List get_all_lugares (Connection conn) { - var res = conn.db.exec (" -SELECT L.id_lugar, L.nombre_lugar, L.valor_entrada, L.nivel, -C.id_ciudad, C.nombre_ciudad, -R.id_region, R.nombre_region -FROM lugar L -JOIN ciudad C ON (L.id_ciudad = C.id_ciudad) -JOIN region R ON (C.id_region = R.id_region) - "); + public static List get_all_lugares (Connection conn) throws PostgresError { + var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_PLACES, null)); if (res.get_status () != ExecStatus.TUPLES_OK) { #if DEBUG error (conn.db.get_error_message ()); @@ -109,6 +103,63 @@ JOIN region R ON (C.id_region = R.id_region) } return list; } + + /** + * Update a place in the database + * @param conn The database connection + * @param lugar The place to update + * @throws PostgresError Thrown if there is a problem with escaping strings + * @throws DBError Thrown if an invalid value is passed + */ + public static void update_place (Connection conn, Lugar lugar) throws PostgresError, DBError { + if (lugar.id_lugar == 0) { + throw new DBError.INVALID_VALUE (_ ("The id of the place is invalid!")); + } + var res = conn.db.exec (Query.get_query (conn, Query.Type.UPDATE_PLACE, lugar)); + if (res.get_status () != ExecStatus.COMMAND_OK) { + #if DEBUG + error (conn.db.get_error_message ()); + #else + warning (conn.db.get_error_message ()); + #endif + } + } + + /** + * Insert a place in the database + * @param conn The database connection + * @param lugar The place to insert + * @return Returns the id of the tuple inserted + * @throws PostgresError Thrown if there is a problem with escaping strings + * @throws DBError Thrown if an invalid value is passed + */ + public static uint insert_place (Connection conn, Lugar lugar) throws PostgresError, DBError { + if (lugar.id_lugar != 0) { + throw new DBError.INVALID_VALUE (_ ("The id of the place is invalid!")); + } + var res = conn.db.exec (Query.get_query (conn, Query.Type.INSERT_PLACE, lugar)); + // This uses TUPLES_OK because it returns a result which is the id of the inserted tour + if (res.get_status () != ExecStatus.TUPLES_OK) { + #if DEBUG + error (conn.db.get_error_message ()); + #else + warning (conn.db.get_error_message ()); + #endif + } + + var wra = new ResultWrapper (res); + try { + return wra.get_int_n (0, "id_lugar"); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + return 0; + #endif + } + } } } } diff --git a/lib/queries.vala b/lib/queries.vala index 615bc8e..80d8b28 100644 --- a/lib/queries.vala +++ b/lib/queries.vala @@ -28,6 +28,9 @@ namespace LibSernatur { INSERT_TOUR, UPDATE_TOUR, DELETE_TOUR, + SELECT_ALL_PLACES, + INSERT_PLACE, + UPDATE_PLACE, SELECT_ALL_ASSOCIATED, SELECT_ALL_ASSOCIATED_BY_TOUR } @@ -118,6 +121,39 @@ RETURNING id_tour"; DELETE FROM tour WHERE id_tour = " + tour.id_tour.to_string (); + case SELECT_ALL_PLACES: + return " +SELECT L.id_lugar, L.nombre_lugar, L.valor_entrada, L.nivel, +C.id_ciudad, C.nombre_ciudad, +R.id_region, R.nombre_region +FROM lugar L +JOIN ciudad C ON (L.id_ciudad = C.id_ciudad) +JOIN region R ON (C.id_region = R.id_region)"; + + case UPDATE_PLACE: + Lugar lugar = (Lugar) t; + return " +UPDATE lugar SET +nombre_lugar = '" + conn.escape (lugar.nombre_lugar) + "', +valor_entrada = " + lugar.valor_entrada.to_string () + ", +nivel = " + lugar.nivel.to_string () + ", +id_ciudad = " + lugar.ciudad.id_ciudad.to_string () + " +WHERE id_lugar = " + lugar.id_lugar.to_string (); + + case INSERT_PLACE: + Lugar lugar = (Lugar) t; + return " +INSERT INTO lugar +(nombre_lugar, valor_entrada, nivel, id_ciudad) +VALUES +( +'" + conn.escape (lugar.nombre_lugar) + "', +" + lugar.valor_entrada.to_string () + ", +" + lugar.nivel.to_string () + ", +" + lugar.ciudad.id_ciudad.to_string () + " +) +RETURNING id_lugar"; + case SELECT_ALL_ASSOCIATED: return " SELECT A.fecha_llegada, A.hora_llegada, A.fecha_salida, A.hora_salida, diff --git a/po/POTFILES b/po/POTFILES index a3e4e73..ac1e98b 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -3,6 +3,7 @@ lib/dbwrapper.vala lib/misc.vala lib/db/asociado.vala lib/db/ciudad.vala +lib/db/lugar.vala lib/db/region.vala lib/db/tour.vala src/sernatur.vala @@ -16,6 +17,7 @@ data/ui/main.splash.ui data/ui/tour.list.ui data/ui/tour.editor.ui data/ui/tour.places.ui +data/ui/place.editor.ui data/ui/query.window.ui data/ui/query.tree.ui data/gschema/cl.cromer.ubb.sernatur.db.gschema.xml diff --git a/po/es.po b/po/es.po index 71f814d..4fa9d65 100644 --- a/po/es.po +++ b/po/es.po @@ -7,8 +7,8 @@ msgid "" msgstr "" "Project-Id-Version: sernatur\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-01-19 17:42-0300\n" -"PO-Revision-Date: 2019-01-19 17:41-0300\n" +"POT-Creation-Date: 2019-01-19 20:02-0300\n" +"PO-Revision-Date: 2019-01-19 20:02-0300\n" "Last-Translator: Chris Cromer \n" "Language-Team: none\n" "Language: es\n" @@ -53,6 +53,10 @@ msgstr "El id del tour es invalido!" msgid "The id of the city is invalid!" msgstr "El id de la ciudad es invalida!" +#: lib/db/lugar.vala:116 lib/db/lugar.vala:138 +msgid "The id of the place is invalid!" +msgstr "El id del lugar es invalida!" + #: lib/db/region.vala:91 msgid "The id of the region is invalid!" msgstr "El id de la región es invalida!" @@ -176,10 +180,12 @@ msgid "Minimum People" msgstr "Mínima Personas" #: data/ui/tour.list.ui:124 data/ui/tour.editor.ui:186 +#: data/ui/place.editor.ui:150 msgid "City" msgstr "Ciudad" #: data/ui/tour.list.ui:138 data/ui/tour.editor.ui:134 +#: data/ui/place.editor.ui:116 msgid "Region" msgstr "Región" @@ -220,15 +226,15 @@ msgstr "Cerrar esta ventana." msgid "Tour Editor" msgstr "Editor de Tour" -#: data/ui/tour.editor.ui:233 +#: data/ui/tour.editor.ui:233 data/ui/place.editor.ui:197 msgid "Create a new region by typing here." msgstr "Crear una nueva región con escribir aquí." -#: data/ui/tour.editor.ui:261 +#: data/ui/tour.editor.ui:261 data/ui/place.editor.ui:225 msgid "Create a new city by typing here." msgstr "Crear una nueva ciudad con escribir aquí." -#: data/ui/tour.editor.ui:286 +#: data/ui/tour.editor.ui:286 data/ui/place.editor.ui:250 msgid "Cancel" msgstr "Cancelar" @@ -244,7 +250,7 @@ msgstr "Lugares" msgid "Add or edit places." msgstr "Agregar o editar places." -#: data/ui/tour.editor.ui:324 +#: data/ui/tour.editor.ui:324 data/ui/place.editor.ui:269 msgid "Save" msgstr "Guardar" @@ -252,15 +258,15 @@ msgstr "Guardar" msgid "Save this tour." msgstr "Guardar este tour." -#: data/ui/tour.places.ui:68 +#: data/ui/tour.places.ui:68 data/ui/place.editor.ui:51 msgid "Place Name" msgstr "Nombre de Lugar" -#: data/ui/tour.places.ui:82 +#: data/ui/tour.places.ui:82 data/ui/place.editor.ui:84 msgid "Ticket Price" msgstr "Valor Entrada" -#: data/ui/tour.places.ui:96 +#: data/ui/tour.places.ui:96 data/ui/place.editor.ui:100 msgid "Difficulty" msgstr "Dificultad" @@ -300,6 +306,18 @@ msgstr "Borrar lugar asociado." msgid "Create a new place to associate." msgstr "Crear un lugar nuevo para asociar con el tour." +#: data/ui/place.editor.ui:27 +msgid "Place Editor" +msgstr "Editor de Lugar" + +#: data/ui/place.editor.ui:254 +msgid "Cancel the modification of this place." +msgstr "Cancelar la modificación de este lugar." + +#: data/ui/place.editor.ui:273 +msgid "Save this place." +msgstr "Guardar este lugar." + #: data/ui/query.window.ui:84 msgid "Run" msgstr "Correr" diff --git a/po/es_CL.po b/po/es_CL.po index f6c71ff..eb288df 100644 --- a/po/es_CL.po +++ b/po/es_CL.po @@ -7,8 +7,8 @@ msgid "" msgstr "" "Project-Id-Version: sernatur\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-01-19 17:42-0300\n" -"PO-Revision-Date: 2019-01-19 17:41-0300\n" +"POT-Creation-Date: 2019-01-19 20:02-0300\n" +"PO-Revision-Date: 2019-01-19 20:04-0300\n" "Last-Translator: Chris Cromer \n" "Language-Team: none\n" "Language: es\n" @@ -53,6 +53,10 @@ msgstr "El id de la wea es invalida!" msgid "The id of the city is invalid!" msgstr "El id de la wea es invalida!" +#: lib/db/lugar.vala:116 lib/db/lugar.vala:138 +msgid "The id of the place is invalid!" +msgstr "El id de la wea es invalida!" + #: lib/db/region.vala:91 msgid "The id of the region is invalid!" msgstr "El id de la wea es invalida!" @@ -176,10 +180,12 @@ msgid "Minimum People" msgstr "Mínima de Weones" #: data/ui/tour.list.ui:124 data/ui/tour.editor.ui:186 +#: data/ui/place.editor.ui:150 msgid "City" msgstr "Ciudad" #: data/ui/tour.list.ui:138 data/ui/tour.editor.ui:134 +#: data/ui/place.editor.ui:116 msgid "Region" msgstr "Región" @@ -220,15 +226,15 @@ msgstr "Cerrar la wea." msgid "Tour Editor" msgstr "Editor de la Wea" -#: data/ui/tour.editor.ui:233 +#: data/ui/tour.editor.ui:233 data/ui/place.editor.ui:197 msgid "Create a new region by typing here." msgstr "Crear una nueva wea con escribir aquí." -#: data/ui/tour.editor.ui:261 +#: data/ui/tour.editor.ui:261 data/ui/place.editor.ui:225 msgid "Create a new city by typing here." msgstr "Crear una nueva wea con escribir aquí." -#: data/ui/tour.editor.ui:286 +#: data/ui/tour.editor.ui:286 data/ui/place.editor.ui:250 msgid "Cancel" msgstr "Cancelar la wea" @@ -244,7 +250,7 @@ msgstr "Lugares" msgid "Add or edit places." msgstr "Agregar o editar places." -#: data/ui/tour.editor.ui:324 +#: data/ui/tour.editor.ui:324 data/ui/place.editor.ui:269 msgid "Save" msgstr "Guardar la wea" @@ -252,15 +258,15 @@ msgstr "Guardar la wea" msgid "Save this tour." msgstr "Guardar esta wea." -#: data/ui/tour.places.ui:68 +#: data/ui/tour.places.ui:68 data/ui/place.editor.ui:51 msgid "Place Name" msgstr "Nombre de la Wea" -#: data/ui/tour.places.ui:82 +#: data/ui/tour.places.ui:82 data/ui/place.editor.ui:84 msgid "Ticket Price" msgstr "Valor de la Wea" -#: data/ui/tour.places.ui:96 +#: data/ui/tour.places.ui:96 data/ui/place.editor.ui:100 msgid "Difficulty" msgstr "Dificultad" @@ -300,6 +306,18 @@ msgstr "Borrar la wea asociado." msgid "Create a new place to associate." msgstr "Crear una wea nueva para asociar con el tour." +#: data/ui/place.editor.ui:27 +msgid "Place Editor" +msgstr "Editor de la Wea" + +#: data/ui/place.editor.ui:254 +msgid "Cancel the modification of this place." +msgstr "Cancelar la modificación de esta wea." + +#: data/ui/place.editor.ui:273 +msgid "Save this place." +msgstr "Guardar esta wea." + #: data/ui/query.window.ui:84 msgid "Run" msgstr "Correr la wea" diff --git a/po/sernatur.pot b/po/sernatur.pot index b4d111a..161f24d 100644 --- a/po/sernatur.pot +++ b/po/sernatur.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: sernatur\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-01-19 17:42-0300\n" +"POT-Creation-Date: 2019-01-19 20:02-0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -50,6 +50,10 @@ msgstr "" msgid "The id of the city is invalid!" msgstr "" +#: lib/db/lugar.vala:116 lib/db/lugar.vala:138 +msgid "The id of the place is invalid!" +msgstr "" + #: lib/db/region.vala:91 msgid "The id of the region is invalid!" msgstr "" @@ -169,10 +173,12 @@ msgid "Minimum People" msgstr "" #: data/ui/tour.list.ui:124 data/ui/tour.editor.ui:186 +#: data/ui/place.editor.ui:150 msgid "City" msgstr "" #: data/ui/tour.list.ui:138 data/ui/tour.editor.ui:134 +#: data/ui/place.editor.ui:116 msgid "Region" msgstr "" @@ -213,15 +219,15 @@ msgstr "" msgid "Tour Editor" msgstr "" -#: data/ui/tour.editor.ui:233 +#: data/ui/tour.editor.ui:233 data/ui/place.editor.ui:197 msgid "Create a new region by typing here." msgstr "" -#: data/ui/tour.editor.ui:261 +#: data/ui/tour.editor.ui:261 data/ui/place.editor.ui:225 msgid "Create a new city by typing here." msgstr "" -#: data/ui/tour.editor.ui:286 +#: data/ui/tour.editor.ui:286 data/ui/place.editor.ui:250 msgid "Cancel" msgstr "" @@ -237,7 +243,7 @@ msgstr "" msgid "Add or edit places." msgstr "" -#: data/ui/tour.editor.ui:324 +#: data/ui/tour.editor.ui:324 data/ui/place.editor.ui:269 msgid "Save" msgstr "" @@ -245,15 +251,15 @@ msgstr "" msgid "Save this tour." msgstr "" -#: data/ui/tour.places.ui:68 +#: data/ui/tour.places.ui:68 data/ui/place.editor.ui:51 msgid "Place Name" msgstr "" -#: data/ui/tour.places.ui:82 +#: data/ui/tour.places.ui:82 data/ui/place.editor.ui:84 msgid "Ticket Price" msgstr "" -#: data/ui/tour.places.ui:96 +#: data/ui/tour.places.ui:96 data/ui/place.editor.ui:100 msgid "Difficulty" msgstr "" @@ -293,6 +299,18 @@ msgstr "" msgid "Create a new place to associate." msgstr "" +#: data/ui/place.editor.ui:27 +msgid "Place Editor" +msgstr "" + +#: data/ui/place.editor.ui:254 +msgid "Cancel the modification of this place." +msgstr "" + +#: data/ui/place.editor.ui:273 +msgid "Save this place." +msgstr "" + #: data/ui/query.window.ui:84 msgid "Run" msgstr "" diff --git a/src/meson.build b/src/meson.build index 0048cab..fede258 100644 --- a/src/meson.build +++ b/src/meson.build @@ -19,18 +19,21 @@ vala_sources = files( 'tour_list.vala', 'tour_editor.vala', 'tour_places.vala', + 'place_editor.vala', 'queries.vala', 'query_window.vala') sources = vala_sources sources += main_gresource sources += tour_gresource +sources += place_gresource sources += query_gresource sources += config_data_file vala_args = ['--vapidir=' + join_paths(meson.source_root(), 'vapi')] vala_args += ['--gresources=' + join_paths(meson.source_root(), 'data/ui/main.gresource.xml')] vala_args += ['--gresources=' + join_paths(meson.source_root(), 'data/ui/tour.gresource.xml')] +vala_args += ['--gresources=' + join_paths(meson.source_root(), 'data/ui/place.gresource.xml')] vala_args += ['--gresources=' + join_paths(meson.source_root(), 'data/ui/query.gresource.xml')] inc = include_directories('../lib', './') diff --git a/src/place_editor.vala b/src/place_editor.vala new file mode 100644 index 0000000..efd68ac --- /dev/null +++ b/src/place_editor.vala @@ -0,0 +1,403 @@ +/* + * Copyright 2018-2019 Chris Cromer + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace Sernatur { + using LibSernatur.DB; + + /** + * The place editor window class + */ + [GtkTemplate (ui = "/cl/cromer/ubb/sernatur/place.editor.ui")] + public class PlaceEditor : Gtk.ApplicationWindow { + /** + * The open database connection + */ + private Connection conn; + /** + * The place to edit + */ + private Lugar lugar; + /** + * The city data stored in the list store + */ + private enum CityColumn { + /** + * The city name + */ + CITY_NAME, + /** + * The city object + */ + CITY, + /** + * The number of colums in this enum + */ + N_COLUMNS + } + /** + * The region data stored in the list store + */ + private enum RegionColumn { + /** + * The region name + */ + REGION_NAME, + /** + * The region object + */ + REGION, + /** + * The number of colums in this enum + */ + N_COLUMNS + } + /** + * The place name + */ + [GtkChild] + private Gtk.Entry place_name; + /** + * The ticket price + */ + [GtkChild] + private Gtk.Entry ticket_price; + /** + * The difficulty + */ + [GtkChild] + private Gtk.SpinButton difficulty; + /** + * The region + */ + [GtkChild] + private Gtk.ComboBoxText region; + /** + * The save button + */ + [GtkChild] + private Gtk.Button save; + /** + * The cancel button + */ + [GtkChild] + private Gtk.Button cancel; + /** + * The city + */ + [GtkChild] + private Gtk.ComboBoxText city; + /** + * The city entry + */ + [GtkChild] + private Gtk.Entry city_entry; + /** + * A list of the cities from the database + */ + private List cities; + /** + * A list of the regions from the database + */ + private List regions; + /** + * The list that stores the regions for the combo box + */ + private Gtk.ListStore region_list_store; + /** + * The list that stores the cities for the combo box + */ + private Gtk.ListStore city_list_store = null; + /** + * This signal is called when a place is saved + */ + public signal void save_place (); + + [GtkCallback] + public void on_changed_combobox (Gtk.ComboBox combobox) { + if (combobox == region) { + Gtk.TreeIter iter; + region.get_active_iter (out iter); + Region temp_region; + if (region_list_store.iter_is_valid (iter)) { + // The region is from the list, not typed + region_list_store.get (iter, + RegionColumn.REGION, out temp_region); + lugar.ciudad.region = temp_region; + if (city_list_store != null && lugar.ciudad.region.id_region != 0) { + reset_city (); + } + } + } + } + + private void reset_city () { + try { + cities = Ciudad.get_all_ciudades_in_region (conn, lugar.ciudad.region.id_region); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + + if (cities.length () > 0) { + cities.sort_with_data ((a, b) => { + return strcmp (a.nombre_ciudad, b.nombre_ciudad); + }); + + if (city_list_store.iter_n_children (null) > 0) { + // If there are more than 0 rows clear it + city_list_store.clear (); + } + cities.foreach ((entry) => { + Gtk.TreeIter iter; + city_list_store.append (out iter); + city_list_store.set (iter, + CityColumn.CITY_NAME, entry.nombre_ciudad, + CityColumn.CITY, entry); + if (lugar.ciudad.id_ciudad == 0) { + lugar.ciudad.id_ciudad = entry.id_ciudad; + lugar.ciudad.nombre_ciudad = entry.nombre_ciudad; + } + else { + if (entry.id_ciudad == lugar.ciudad.id_ciudad) { + city.set_active_iter (iter); + } + } + }); + } + + city.set_active (-1); + city_entry.set_text (""); + } + + /** + * This callback is called when a button is clicked + * @param button The button that was clicked + */ + [GtkCallback] + public void on_clicked_button (Gtk.Button button) { + if (button == save) { + if (lugar.id_lugar == 0) { + update_place_instance (); + try { + Lugar.insert_place (conn, lugar); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + finally { + save_place (); // Signal the parent to update itself + this.close (); + } + } + else { + update_place_instance (); + try { + Lugar.update_place (conn, lugar); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + finally { + save_place (); // Signal the parent to update itself + this.close (); + } + } + } + else if (button == cancel) { + this.close (); + } + } + + /** + * Update the the place object with new info from the editor + */ + private void update_place_instance () { + lugar.nombre_lugar = place_name.get_text (); + lugar.valor_entrada = (uint) int.parse (ticket_price.get_text ()); + lugar.nivel = (uint) difficulty.get_value_as_int (); + if (region.get_active () == -1) { + Region new_region = new Region (0, region.get_active_text ()); + try { + new_region.id_region = Region.insert_region (conn, new_region); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + finally { + update_place_instance_city (new_region); + } + } + else { + Region new_region; + Gtk.TreeIter iter; + region.get_active_iter (out iter); + if (region_list_store.iter_is_valid (iter)) { + region_list_store.get (iter, + RegionColumn.REGION, out new_region); + } + else { + new_region = new Region (); + } + update_place_instance_city (new_region); + } + } + + /** + * This method updates the city part of the lugar instance + * @param new_region The region to insert into the city object + */ + private void update_place_instance_city (Region new_region) { + Ciudad ciudad; + if (city.get_active () == -1) { + ciudad = new Ciudad (0, city.get_active_text ()); + ciudad.region = new_region; + try { + ciudad.id_ciudad = Ciudad.insert_city (conn, ciudad); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + finally { + lugar.ciudad = ciudad; + } + } + else { + Gtk.TreeIter iter; + city.get_active_iter (out iter); + if (city_list_store.iter_is_valid (iter)) { + city_list_store.get (iter, + CityColumn.CITY, out ciudad); + ciudad.region = new_region; + lugar.ciudad = ciudad; + } + } + } + + /** + * Initialize the place editor class + * @param application The application used to make the GLib object + * @param conn The database connection to use + * @param lugar The place to edit + */ + public PlaceEditor (Gtk.Application application, Connection conn, Lugar? lugar) { + Object (application: application); + this.conn = conn; + this.lugar = lugar; + } + + /** + * Initialize what is needed for this window + */ + public void initialize () { + try { + regions = Region.get_all_regiones (conn); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + + regions.sort_with_data ((a, b) => { + return strcmp (a.nombre_region, b.nombre_region); + }); + + region_list_store = new Gtk.ListStore (RegionColumn.N_COLUMNS, + typeof (string), + typeof (Region)); + + region.set_model (region_list_store); + + if (lugar != null) { + place_name.set_text (lugar.nombre_lugar); + ticket_price.set_text (lugar.valor_entrada.to_string ()); + difficulty.set_text (lugar.nivel.to_string ()); + } + else { + lugar = new Lugar (); + lugar.ciudad = new Ciudad (); + lugar.ciudad.region = new Region (); + } + + regions.foreach ((entry) => { + Gtk.TreeIter iter; + region_list_store.append (out iter); + region_list_store.set (iter, + RegionColumn.REGION_NAME, entry.nombre_region, + RegionColumn.REGION, entry); + if (entry.id_region == lugar.ciudad.region.id_region) { + region.set_active_iter (iter); + } + }); + + region.set_entry_text_column (RegionColumn.REGION_NAME); + + city_list_store = new Gtk.ListStore (CityColumn.N_COLUMNS, + typeof (string), + typeof (Ciudad)); + + city.set_model (city_list_store); + + try { + cities = Ciudad.get_all_ciudades_in_region (conn, lugar.ciudad.region.id_region); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + + if (cities.length () > 0) { + cities.sort_with_data ((a, b) => { + return strcmp (a.nombre_ciudad, b.nombre_ciudad); + }); + + cities.foreach ((entry) => { + Gtk.TreeIter iter; + city_list_store.append (out iter); + city_list_store.set (iter, + CityColumn.CITY_NAME, entry.nombre_ciudad, + CityColumn.CITY, entry); + if (entry.id_ciudad == lugar.ciudad.id_ciudad) { + city.set_active_iter (iter); + } + }); + } + } + } +} diff --git a/src/tour_places.vala b/src/tour_places.vala index a355344..793b7e5 100644 --- a/src/tour_places.vala +++ b/src/tour_places.vala @@ -176,78 +176,20 @@ namespace Sernatur { [GtkCallback] private void on_clicked_button (Gtk.Button button) { if (button == new_place) { - /*var tour_editor = new TourEditor (application, conn, null); - tour_editor.set_transient_for (this); // Set this window as the parent of the new window - tour_editor.initialize (); - tour_editor.show_all ();*/ + var place_editor = new PlaceEditor (application, conn, null); + place_editor.set_transient_for (this); // Set this window as the parent of the new window + place_editor.initialize (); + place_editor.show_all (); + place_editor.save_place.connect (on_save); } else if (button == add_place) { } else if (button == edit_place) { - selection.selected_foreach ((model, path, iter) => { - Asociado associated; - model.get (iter, - Column.ASSOCIATED, out associated); - print (associated.lugar.nombre_lugar + "\n"); - }); - /*if (selection.get_selected (out model, out iter)) { - Asociado associated; - model.get (iter, - Column.ASSOCIATED, out associated); - var tour_editor = new TourEditor (application, conn, tour); - tour_editor.set_transient_for (this); // Set this window as the parent of the new window - tour_editor.initialize (); - tour_editor.show_all (); - tour_editor.save_tour.connect (on_save); - }*/ + } else if (button == delete_place) { - /*var msg = new Gtk.MessageDialog (this, - Gtk.DialogFlags.MODAL, - Gtk.MessageType.ERROR, - Gtk.ButtonsType.YES_NO, - _ ("Are you sure you wish to delete this tour?")); - msg.response.connect ((response_id) => { - switch (response_id) { - case Gtk.ResponseType.YES: - try { - Gtk.TreeModel model; - Gtk.TreeIter iter; - Tour tour; - if (selection.get_selected (out model, out iter)) { - model.get (iter, - Column.TOUR, out tour); - Tour.delete_tour (conn, tour); - } - } - catch (DBError e) { - if (e.code == 1) { - var msg2 = new Gtk.MessageDialog (this, - Gtk.DialogFlags.MODAL, - Gtk.MessageType.ERROR, - Gtk.ButtonsType.CLOSE, - _ ("Error: Could not delete tour because there are still associated arrival and departure dates and times!")); - msg2.response.connect ((response_id) => { - msg2.destroy (); - }); - msg2.set_title (_ ("Error")); - msg2.show (); - } - else { - #if DEBUG - error (e.message); - #else - warning (e.message); - #endif - } - } - break; - } - msg.destroy (); - }); - msg.set_title (_ ("Error")); - msg.show ();*/ + } else if (button == close_place) { this.close (); @@ -485,6 +427,20 @@ namespace Sernatur { departure_time.sort_order = Gtk.SortType.DESCENDING; } + /** + * Called when a new or old place is saved + * @param place_editor The editor that saved the place + */ + public void on_save(PlaceEditor place_editor) { + edit_place.sensitive = false; + delete_place.sensitive = false; + reset_columns (); + + list_store.clear (); + + update_list_store (); + } + /** * Update the list store with the data from the database */