summaryrefslogtreecommitdiff
path: root/src/libaudqt/fileopener.cc
blob: c4d6b5c9476aadf24e687a6068b819c3a2bc329d (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
/*
 * fileopener.cc
 * Copyright 2014 Michał Lipski
 *
 * 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 <QFileDialog>

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

#include <libaudqt/libaudqt.h>

namespace audqt {

static aud::array<FileMode, QFileDialog *> s_dialogs;

EXPORT void fileopener_show (FileMode mode)
{
    QFileDialog * & dialog = s_dialogs[mode];

    if (! dialog)
    {
        static constexpr aud::array<FileMode, const char *> titles {
            N_("Open Files"),
            N_("Open Folder"),
            N_("Add Files"),
            N_("Add Folder")
        };

        static constexpr aud::array<FileMode, const char *> labels {
            N_("Open"),
            N_("Open"),
            N_("Add"),
            N_("Add")
        };

        static constexpr aud::array<FileMode, QFileDialog::FileMode> modes {
            QFileDialog::ExistingFiles,
            QFileDialog::Directory,
            QFileDialog::ExistingFiles,
            QFileDialog::Directory
        };

        String path = aud_get_str ("audgui", "filesel_path");
        dialog = new QFileDialog (nullptr, _(titles[mode]), QString (path));

        dialog->setAttribute (Qt::WA_DeleteOnClose);
        dialog->setFileMode (modes[mode]);
        dialog->setLabelText (QFileDialog::Accept, _(labels[mode]));

        QObject::connect (dialog, & QFileDialog::directoryEntered, [] (const QString & path)
            { aud_set_str ("audgui", "filesel_path", path.toUtf8 ().constData ()); });

        QObject::connect (dialog, & QFileDialog::accepted, [dialog, mode] ()
        {
            Index<PlaylistAddItem> files;
            for (const QUrl & url : dialog->selectedUrls ())
                files.append (String (url.toEncoded ().constData ()));

            if (mode == FileMode::Add || mode == FileMode::AddFolder)
                aud_drct_pl_add_list (std::move (files), -1);
            else
                aud_drct_pl_open_list (std::move (files));
        });

        QObject::connect (dialog, & QObject::destroyed, [& dialog] ()
            { dialog = nullptr; });
    }

    window_bring_to_front (dialog);
}

} // namespace audqt