summaryrefslogtreecommitdiff
path: root/src/libaudqt/url-opener-qt.cc
blob: 15421429b974c93a17eabd9016a4ede8543c3721 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * url-opener.cc
 * Copyright 2015 Thomas Lange
 *
 * 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 <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QLabel>
#include <QPointer>
#include <QPushButton>
#include <QVBoxLayout>

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

#include "libaudqt.h"

namespace audqt
{

static QDialog * buildUrlDialog(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";
    }

    auto dialog = new QDialog;
    dialog->setWindowTitle(title);
    dialog->setContentsMargins(margins.EightPt);

    auto label = new QLabel(_("Enter URL:"), dialog);

    auto combobox = new QComboBox(dialog);
    combobox->setEditable(true);
    combobox->setMinimumContentsLength(50);

    auto clear_button =
        new QPushButton(translate_str(N_("C_lear history")), dialog);
    clear_button->setIcon(audqt::get_icon("edit-clear"));

    auto hbox = make_hbox(nullptr);
    prefs_populate(hbox, widgets, PACKAGE);
    hbox->addStretch(1);
    hbox->addWidget(clear_button);

    auto button1 = new QPushButton(translate_str(verb), dialog);
    button1->setIcon(audqt::get_icon(icon));

    auto button2 = new QPushButton(translate_str(N_("_Cancel")), dialog);
    button2->setIcon(audqt::get_icon("process-stop"));

    auto buttonbox = new QDialogButtonBox(dialog);
    buttonbox->addButton(button1, QDialogButtonBox::AcceptRole);
    buttonbox->addButton(button2, QDialogButtonBox::RejectRole);

    auto layout = make_vbox(dialog);
    layout->addWidget(label);
    layout->addWidget(combobox);
    layout->addLayout(hbox);
    layout->addStretch(1);
    layout->addWidget(buttonbox);

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

        combobox->addItem(QString(item));
    }
    combobox->setCurrentIndex(-1);

    QObject::connect(clear_button, &QPushButton::pressed, [combobox]() {
        combobox->clear();
        aud_history_clear();
    });

    QObject::connect(buttonbox, &QDialogButtonBox::rejected, dialog,
                     &QDialog::close);

    QObject::connect(buttonbox, &QDialogButtonBox::accepted,
                     [dialog, combobox, open]() {
                         QByteArray url = combobox->currentText().toUtf8();

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

                         if (aud_get_bool("save_url_history"))
                             aud_history_add(url);

                         dialog->close();
                     });

    return dialog;
}

static QPointer<QDialog> s_dialog;

EXPORT void urlopener_show(bool open)
{
    if (!s_dialog)
    {
        s_dialog = buildUrlDialog(open);
        s_dialog->setAttribute(Qt::WA_DeleteOnClose);
    }

    window_bring_to_front(s_dialog);
}

} // namespace audqt