summaryrefslogtreecommitdiff
path: root/src/libaudgui/url-opener.cc
blob: c994b7adb0a51e81e1bb552e522e8a33bda28d9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * url-opener.c
 * Copyright 2012-2013 John Lindgren
 *
 * 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
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include <gtk/gtk.h>

#include <libaudcore/drct.h>
#include <libaudcore/i18n.h>
#include <libaudcore/preferences.h>
#include <libaudcore/runtime.h>

#include "internal.h"
#include "libaudgui.h"
#include "libaudgui-gtk.h"

static void open_cb (void * entry)
{
    const char * text = gtk_entry_get_text ((GtkEntry *) entry);
    gboolean open = GPOINTER_TO_INT (g_object_get_data ((GObject *) entry, "open"));

    if (open)
        aud_drct_pl_open (text);
    else
        aud_drct_pl_add (text, -1);

    if (aud_get_bool ("save_url_history"))
        aud_history_add (text);
}

static void clear_cb (void * combo)
{
    /* no gtk_combo_box_text_clear()? */
    gtk_list_store_clear ((GtkListStore *) gtk_combo_box_get_model ((GtkComboBox *) combo));
    aud_history_clear ();
}

static GtkWidget * create_url_opener (bool open)
{
    static const PreferencesWidget widgets[] = {
        WidgetCheck (N_("_Save to history"),
            WidgetBool (0, "save_url_history"))
    };

    const char * title, * verb, * icon;

    if (open)
    {
        title = _("Open URL");
        verb = _("_Open");
        icon = "document-open";
    }
    else
    {
        title = _("Add URL");
        verb = _("_Add");
        icon = "list-add";
    }

    GtkWidget * combo = gtk_combo_box_text_new_with_entry ();
    GtkWidget * entry = gtk_bin_get_child ((GtkBin *) combo);
    gtk_entry_set_activates_default ((GtkEntry *) entry, true);

    for (int i = 0;; i++)
    {
        String item = aud_history_get (i);
        if (! item)
            break;

        gtk_combo_box_text_append_text ((GtkComboBoxText *) combo, item);
    }

    g_object_set_data ((GObject *) entry, "open", GINT_TO_POINTER (open));

    GtkWidget * hbox = gtk_hbox_new (false, 6);
    audgui_create_widgets (hbox, widgets);

    GtkWidget * clear_button = audgui_button_new (_("C_lear history"),
     "edit-clear", clear_cb, combo);
    gtk_box_pack_end ((GtkBox *) hbox, clear_button, false, false, 0);

    GtkWidget * vbox = gtk_vbox_new (false, 6);
    gtk_box_pack_start ((GtkBox *) vbox, combo, false, false, 0);
    gtk_box_pack_start ((GtkBox *) vbox, hbox, false, false, 0);

    GtkWidget * button1 = audgui_button_new (verb, icon, open_cb, entry);
    GtkWidget * button2 = audgui_button_new (_("_Cancel"), "process-stop", nullptr, nullptr);

    GtkWidget * dialog = audgui_dialog_new (GTK_MESSAGE_OTHER, title,
     _("Enter URL:"), button1, button2);
    gtk_widget_set_size_request (dialog, 4 * audgui_get_dpi (), -1);
    audgui_dialog_add_widget (dialog, vbox);

    return dialog;
}

EXPORT void audgui_show_add_url_window (bool open)
{
    audgui_show_unique_window (AUDGUI_URL_OPENER_WINDOW, create_url_opener (open));
}