app: reuse GimpLanguageComboBox to show available manual languages.

Better factorize by reusing code rather than recreating a combo box
which does basically the same thing. I only added a boolean parameter to
retrieve only the sublist of manual language.
It also takes advantage of the self-translated language names from
initialization.
This commit is contained in:
Jehan 2017-10-03 01:52:35 +02:00
parent d0bb3efff4
commit 6608196978
7 changed files with 172 additions and 135 deletions

View File

@ -47,6 +47,7 @@
#include "gimphelp.h"
#include "gimphelp-ids.h"
#include "gimplanguagecombobox.h"
#include "gimplanguagestore-parser.h"
#include "gimpmessagebox.h"
#include "gimpmessagedialog.h"
@ -101,7 +102,6 @@ static GFile * gimp_help_get_user_manual_basedir (void);
static void gimp_help_query_alt_user_manual (GimpIdleHelp *idle_help);
static GList * gimp_help_get_installed_manuals (Gimp *gimp);
static void gimp_help_language_combo_changed (GtkComboBox *combo,
GimpIdleHelp *idle_help);
@ -227,6 +227,61 @@ gimp_help_user_manual_changed (Gimp *gimp)
}
}
GList *
gimp_help_get_installed_languages (void)
{
GList *manuals = NULL;
GFile *basedir;
/* if GIMP2_HELP_URI is set, assume that the manual can be found there */
if (g_getenv ("GIMP2_HELP_URI"))
basedir = g_file_new_for_uri (g_getenv ("GIMP2_HELP_URI"));
else
basedir = gimp_help_get_user_manual_basedir ();
if (g_file_query_file_type (basedir, G_FILE_QUERY_INFO_NONE, NULL) ==
G_FILE_TYPE_DIRECTORY)
{
GFileEnumerator *enumerator;
enumerator = g_file_enumerate_children (basedir,
G_FILE_ATTRIBUTE_STANDARD_NAME ","
G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
if (enumerator)
{
GFileInfo *info;
while ((info = g_file_enumerator_next_file (enumerator,
NULL, NULL)))
{
if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
{
GFile *locale_dir;
GFile *file;
locale_dir = g_file_enumerator_get_child (enumerator, info);
file = g_file_get_child (locale_dir, "gimp-help.xml");
if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE,
NULL) == G_FILE_TYPE_REGULAR)
{
manuals = g_list_prepend (manuals,
g_strdup (g_file_info_get_name (info)));
}
g_object_unref (locale_dir);
g_object_unref (file);
}
g_object_unref (info);
}
g_object_unref (enumerator);
}
}
g_object_unref (basedir);
return manuals;
}
/* private functions */
@ -755,64 +810,23 @@ gimp_help_query_alt_user_manual (GimpIdleHelp *idle_help)
"in your language."));
/* Add a list of available manuals instead, if any. */
manuals = gimp_help_get_installed_manuals (idle_help->gimp);
manuals = gimp_help_get_installed_languages ();
if (manuals != NULL)
{
GtkWidget *lang_combo;
GtkTreeStore *store;
GtkCellRenderer *cell;
GList *lang;
GHashTable *languages;
GtkTreeIter tree_iter;
GtkWidget *lang_combo;
languages = gimp_language_store_parser_get_languages (FALSE);
/* Add an additional button. */
gtk_dialog_add_button (GTK_DIALOG (dialog),
_("Read Selected _Language"),
GTK_RESPONSE_YES);
/* And a dropdown list of available manuals. */
store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
/* First item is for labelling. */
gtk_tree_store_insert (store, &tree_iter, NULL, -1);
gtk_tree_store_set (store, &tree_iter,
0, _("Available manuals"),
1, "",
-1);
for (lang = manuals; lang; lang = lang->next)
{
const gchar *localized_language;
gchar *label = NULL;
localized_language = g_hash_table_lookup (languages,
lang->data);
if (localized_language)
{
label = g_strdup_printf ("%s [%s]", localized_language,
(gchar *) lang->data);
}
gtk_tree_store_insert (store, &tree_iter, NULL, -1);
gtk_tree_store_set (store, &tree_iter,
0, label ? label : lang->data,
1, lang->data,
-1);
if (label)
g_free (label);
}
lang_combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
g_object_unref (store);
cell = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (lang_combo), cell,
TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (lang_combo), cell,
"text", 0, NULL);
lang_combo = gimp_language_combo_box_new (TRUE);
gtk_combo_box_set_active (GTK_COMBO_BOX (lang_combo), 0);
gtk_dialog_set_response_sensitive (idle_help->query_dialog,
GTK_RESPONSE_YES, FALSE);
g_signal_connect (lang_combo, "changed",
G_CALLBACK (gimp_help_language_combo_changed),
idle_help);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
lang_combo, TRUE, TRUE, 0);
gtk_widget_show (lang_combo);
@ -853,90 +867,17 @@ gimp_help_query_alt_user_manual (GimpIdleHelp *idle_help)
gtk_widget_show (dialog);
}
static GList *
gimp_help_get_installed_manuals (Gimp *gimp)
{
GList *manuals = NULL;
GFile *basedir;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
/* if GIMP2_HELP_URI is set, assume that the manual can be found there */
if (g_getenv ("GIMP2_HELP_URI"))
basedir = g_file_new_for_uri (g_getenv ("GIMP2_HELP_URI"));
else
basedir = gimp_help_get_user_manual_basedir ();
if (g_file_query_file_type (basedir, G_FILE_QUERY_INFO_NONE, NULL) ==
G_FILE_TYPE_DIRECTORY)
{
GFileEnumerator *enumerator;
enumerator = g_file_enumerate_children (basedir,
G_FILE_ATTRIBUTE_STANDARD_NAME ","
G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
if (enumerator)
{
GFileInfo *info;
while ((info = g_file_enumerator_next_file (enumerator,
NULL, NULL)))
{
if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
{
GFile *locale_dir;
GFile *file;
locale_dir = g_file_enumerator_get_child (enumerator, info);
file = g_file_get_child (locale_dir, "gimp-help.xml");
if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE,
NULL) == G_FILE_TYPE_REGULAR)
{
manuals = g_list_prepend (manuals,
g_strdup (g_file_info_get_name (info)));
}
g_object_unref (locale_dir);
g_object_unref (file);
}
g_object_unref (info);
}
g_object_unref (enumerator);
}
}
g_object_unref (basedir);
return manuals;
}
static void
gimp_help_language_combo_changed (GtkComboBox *combo,
GimpIdleHelp *idle_help)
{
gchar *help_locales = NULL;
GtkTreeIter iter;
gchar *help_locales = NULL;
gchar *code;
if (gtk_combo_box_get_active_iter (combo, &iter))
code = gimp_language_combo_box_get_code (GIMP_LANGUAGE_COMBO_BOX (combo));
if (code && g_strcmp0 ("", code) != 0)
{
GtkTreeModel *model;
GValue value = { 0, };
model = gtk_combo_box_get_model (combo);
gtk_tree_model_get_value (model, &iter, 1, &value);
if (g_strcmp0 ("", g_value_get_string (&value)) != 0)
help_locales = g_strdup_printf ("%s:",
g_value_get_string (&value));
g_value_unset (&value);
}
g_object_set (idle_help->gimp->config,
"help-locales", help_locales? help_locales : "",
NULL);
if (help_locales)
{
g_free (help_locales);
help_locales = g_strdup_printf ("%s:", code);
gtk_dialog_set_response_sensitive (idle_help->query_dialog,
GTK_RESPONSE_YES, TRUE);
}
@ -945,4 +886,11 @@ gimp_help_language_combo_changed (GtkComboBox *combo,
gtk_dialog_set_response_sensitive (idle_help->query_dialog,
GTK_RESPONSE_YES, FALSE);
}
g_object_set (idle_help->gimp->config,
"help-locales", help_locales? help_locales : "",
NULL);
g_free (code);
if (help_locales)
g_free (help_locales);
}

View File

@ -46,4 +46,6 @@ gboolean gimp_help_user_manual_is_installed (Gimp *gimp);
void gimp_help_user_manual_changed (Gimp *gimp);
GList * gimp_help_get_installed_languages (void);
#endif /* __GIMP_HELP_H__ */

View File

@ -65,12 +65,12 @@ gimp_language_combo_box_init (GimpLanguageComboBox *combo)
}
GtkWidget *
gimp_language_combo_box_new (void)
gimp_language_combo_box_new (gboolean manual_l18n)
{
GtkWidget *combo;
GtkListStore *store;
store = gimp_translation_store_new ();
store = gimp_translation_store_new (manual_l18n);
combo = g_object_new (GIMP_TYPE_LANGUAGE_COMBO_BOX,
"model", store,

View File

@ -40,7 +40,7 @@ struct _GimpLanguageComboBoxClass
GType gimp_language_combo_box_get_type (void) G_GNUC_CONST;
GtkWidget * gimp_language_combo_box_new (void);
GtkWidget * gimp_language_combo_box_new (gboolean manual_l18n);
gchar * gimp_language_combo_box_get_code (GimpLanguageComboBox *combo);
gboolean gimp_language_combo_box_set_code (GimpLanguageComboBox *combo,

View File

@ -1351,7 +1351,7 @@ gimp_prop_language_combo_box_new (GObject *config,
if (! param_spec)
return NULL;
combo = gimp_language_combo_box_new ();
combo = gimp_language_combo_box_new (FALSE);
g_object_get (config,
property_name, &value,

View File

@ -18,6 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <locale.h>
#include <string.h>
@ -27,9 +29,17 @@
#include "widgets-types.h"
#include "gimphelp.h"
#include "gimplanguagestore-parser.h"
#include "gimptranslationstore.h"
#include "gimp-intl.h"
enum
{
PROP_0,
PROP_MANUAL_L18N
};
struct _GimpTranslationStoreClass
{
@ -39,10 +49,20 @@ struct _GimpTranslationStoreClass
struct _GimpTranslationStore
{
GimpLanguageStore parent_instance;
gboolean manual_l18n;
};
static void gimp_translation_store_constructed (GObject *object);
static void gimp_translation_store_constructed (GObject *object);
static void gimp_translation_store_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_translation_store_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
G_DEFINE_TYPE (GimpTranslationStore, gimp_translation_store,
@ -54,9 +74,17 @@ G_DEFINE_TYPE (GimpTranslationStore, gimp_translation_store,
static void
gimp_translation_store_class_init (GimpTranslationStoreClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = gimp_translation_store_constructed;
object_class->constructed = gimp_translation_store_constructed;
object_class->set_property = gimp_translation_store_set_property;
object_class->get_property = gimp_translation_store_get_property;
g_object_class_install_property (object_class, PROP_MANUAL_L18N,
g_param_spec_boolean ("manual-l18n", NULL, NULL,
FALSE,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
}
static void
@ -71,19 +99,78 @@ gimp_translation_store_constructed (GObject *object)
GHashTableIter lang_iter;
gpointer code;
gpointer name;
GList *sublist = NULL;
lang_list = gimp_language_store_parser_get_languages (TRUE);
g_return_if_fail (lang_list != NULL);
if (GIMP_TRANSLATION_STORE (object)->manual_l18n)
sublist = gimp_help_get_installed_languages ();
g_hash_table_iter_init (&lang_iter, lang_list);
if (sublist)
{
GIMP_LANGUAGE_STORE_GET_CLASS (object)->add (GIMP_LANGUAGE_STORE (object),
_("Available manuals..."),
"");
}
while (g_hash_table_iter_next (&lang_iter, &code, &name))
GIMP_LANGUAGE_STORE_GET_CLASS (object)->add (GIMP_LANGUAGE_STORE (object),
name, code);
{
if (! sublist ||
g_list_find_custom (sublist, code, (GCompareFunc) g_strcmp0))
{
GIMP_LANGUAGE_STORE_GET_CLASS (object)->add (GIMP_LANGUAGE_STORE (object),
name, code);
}
}
g_list_free_full (sublist, g_free);
}
static void
gimp_translation_store_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GimpTranslationStore *store = GIMP_TRANSLATION_STORE (object);
switch (property_id)
{
case PROP_MANUAL_L18N:
store->manual_l18n = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_translation_store_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GimpTranslationStore *store = GIMP_TRANSLATION_STORE (object);
switch (property_id)
{
case PROP_MANUAL_L18N:
g_value_set_boolean (value, store->manual_l18n);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
GtkListStore *
gimp_translation_store_new (void)
gimp_translation_store_new (gboolean manual_l18n)
{
return g_object_new (GIMP_TYPE_TRANSLATION_STORE, NULL);
return g_object_new (GIMP_TYPE_TRANSLATION_STORE,
"manual-l18n", manual_l18n,
NULL);
}

View File

@ -38,7 +38,7 @@ typedef struct _GimpTranslationStoreClass GimpTranslationStoreClass;
GType gimp_translation_store_get_type (void) G_GNUC_CONST;
GtkListStore * gimp_translation_store_new (void);
GtkListStore * gimp_translation_store_new (gboolean manual_l18n);
#endif /* __GIMP_TRANSLATION_STORE_H__ */