-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathApplication.vala
More file actions
188 lines (148 loc) · 5.89 KB
/
Application.vala
File metadata and controls
188 lines (148 loc) · 5.89 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
* SPDX-FileCopyrightText: 2021 elementary, Inc. (https://elementary.io)
*/
public class Music.Application : Gtk.Application {
public const string ACTION_PREFIX = "app.";
public const string ACTION_NEXT = "action-next";
public const string ACTION_PLAY_PAUSE = "action-play-pause";
public const string ACTION_PREVIOUS = "action-previous";
public const string ACTION_SHUFFLE = "action-shuffle";
public const string ACTION_FIND = "action-find";
public const string ACTION_CLEAR_QUEUE = "action-clear-queue";
public const string ACTION_QUIT = "action-quit";
private const ActionEntry[] ACTION_ENTRIES = {
{ ACTION_FIND, action_find },
{ ACTION_QUIT, quit }
};
private PlaybackManager? playback_manager = null;
public Application () {
Object (
application_id: "io.elementary.music",
flags: ApplicationFlags.HANDLES_OPEN
);
}
construct {
GLib.Intl.setlocale (LocaleCategory.ALL, "");
GLib.Intl.bindtextdomain (Constants.GETTEXT_PACKAGE, Constants.LOCALEDIR);
GLib.Intl.bind_textdomain_codeset (Constants.GETTEXT_PACKAGE, "UTF-8");
GLib.Intl.textdomain (Constants.GETTEXT_PACKAGE);
}
protected override void startup () {
base.startup ();
Granite.init ();
playback_manager = PlaybackManager.get_default ();
add_action_entries (ACTION_ENTRIES, this);
set_accels_for_action (ACTION_PREFIX + ACTION_FIND, {"<Ctrl>F"});
set_accels_for_action (ACTION_PREFIX + ACTION_QUIT, {"<Ctrl>Q"});
var granite_settings = Granite.Settings.get_default ();
var gtk_settings = Gtk.Settings.get_default ();
gtk_settings.gtk_icon_theme_name = "elementary";
gtk_settings.gtk_theme_name = "io.elementary.stylesheet.orange";
gtk_settings.gtk_application_prefer_dark_theme = (
granite_settings.prefers_color_scheme == DARK
);
granite_settings.notify["prefers-color-scheme"].connect (() => {
gtk_settings.gtk_application_prefer_dark_theme = (
granite_settings.prefers_color_scheme == DARK
);
});
}
protected override void activate () {
if (active_window != null) {
active_window.present ();
return;
}
var mpris_id = Bus.own_name (
BusType.SESSION,
"org.mpris.MediaPlayer2.io.elementary.music",
BusNameOwnerFlags.NONE,
on_bus_acquired,
null,
null
);
if (mpris_id == 0) {
warning ("Could not initialize MPRIS session.\n");
}
var main_window = new MainWindow () {
title = _("Music")
};
main_window.present ();
add_window (main_window);
/*
* This is very finicky. Bind size after present else set_titlebar gives us bad sizes
* Set maximize after height/width else window is min size on unmaximize
* Bind maximize as SET else get get bad sizes
*/
var settings = new Settings ("io.elementary.music");
settings.bind ("window-height", main_window, "default-height", SettingsBindFlags.DEFAULT);
settings.bind ("window-width", main_window, "default-width", SettingsBindFlags.DEFAULT);
if (settings.get_boolean ("window-maximized")) {
main_window.maximize ();
}
settings.bind ("window-maximized", main_window, "maximized", SettingsBindFlags.SET);
// This needs to be done after window is constructed
// Else music plays but the queue seems empty
playback_manager.restore_queue ();
}
private static File[] list_directory (string directory) {
Dir dir;
try {
dir = Dir.open (directory, 0);
} catch (FileError e) {
warning (e.message);
return {};
}
string? name = null;
File[] elements = {};
while ((name = dir.read_name ()) != null) {
var file_path = Path.build_filename (directory, name);
var file = File.new_for_path (file_path);
elements += file;
}
return elements;
}
/**
* Flattens files and those contained in (sub)directories
*/
public static File[] loop_through_files (File[] files) {
// All of these will be returned later in bulk
File[] elements = {};
foreach (var file in files) {
var file_path = file.get_path ();
if (FileUtils.test (file_path, FileTest.IS_DIR)) {
var directory_elements = list_directory (file_path);
var directory_files = loop_through_files (directory_elements);
foreach (var directory_file in directory_files) {
elements += directory_file;
}
continue;
}
elements += file;
}
return elements;
}
protected override void open (File[] files, string hint) {
if (active_window == null) {
activate ();
}
var files_to_play = loop_through_files (files);
debug ("Application: Number of files to play %u", files_to_play.length);
playback_manager.queue_files (files_to_play);
}
private void action_find () {
((MainWindow)active_window).start_search ();
}
private void on_bus_acquired (DBusConnection connection, string name) {
try {
connection.register_object ("/org/mpris/MediaPlayer2", new MprisRoot ());
connection.register_object ("/org/mpris/MediaPlayer2", new MprisPlayer (connection));
} catch (IOError e) {
warning ("could not create MPRIS player: %s\n", e.message);
}
}
public static int main (string[] args) {
Gst.init (ref args);
return new Music.Application ().run (args);
}
}