///| Default apps: a temporary XDG tree (applications, mimeinfo.cache, mimeapps.list, /// xdg-terminals.list), the categories' choices and current defaults, setting them (other entries /// kept), the terminal choice, and the settings module's Apps tab. ///| fn da_write(path : String, text : String) -> Unit { let slash = path.rev_find("/").unwrap() ignore(@sys.mkdir_p(path[:slash].to_string())) ignore(@sys.write_file_bytes(path, @sys.str_to_utf8_bytes(text))) } ///| fn da_read(path : String) -> String { let raw = @sys.read_file_bytes(path) let sb = StringBuilder::new() for i = 0; i < raw.length(); i = i + 1 { sb.write_char(Int::unsafe_to_char(raw[i].to_int())) // the files are ASCII } sb.to_string() } ///| A desktop entry. fn da_app(name : String, exec : String, mimes : String, cats : String, extra? : String = "") -> String { "[Desktop Entry]\nType=Application\nName=" + name + "\nExec=" + exec + "\nMimeType=" + mimes + "\nCategories=" + cats + "\n" + extra } ///| A fresh XDG tree under /tmp: user config + data, one system config dir, one system data dir. fn da_tree(tag : String) -> (String, @shell.XdgDirs) { let root = "/tmp/chandra-default-apps-" + tag + "-" + @sys.getpid().to_string() let sys = root + "/usr/share/applications" da_write(sys + "/firefox.desktop", da_app("Firefox", "firefox %u", "text/html;x-scheme-handler/http;x-scheme-handler/https;image/png;application/pdf;", "Network;WebBrowser;")) da_write(sys + "/chromium.desktop", da_app("Chromium", "chromium %U", "text/html;x-scheme-handler/http;x-scheme-handler/https;", "Network;WebBrowser;")) da_write(sys + "/thunderbird.desktop", da_app("Thunderbird", "thunderbird %u", "x-scheme-handler/mailto;message/rfc822;", "Network;Email;")) da_write(sys + "/org.gnome.Nautilus.desktop", da_app("Files", "nautilus --new-window %U", "inode/directory;", "GNOME;Utility;FileManager;")) da_write(sys + "/foot.desktop", da_app("Foot", "foot", "", "System;TerminalEmulator;")) da_write(sys + "/kitty.desktop", da_app("kitty", "kitty", "", "System;TerminalEmulator;")) da_write(sys + "/org.gnome.gedit.desktop", da_app("Text Editor", "gedit %U", "text/plain;text/markdown;", "Utility;TextEditor;")) da_write(sys + "/eog.desktop", da_app("Image Viewer", "eog %U", "image/png;image/jpeg;", "Graphics;Viewer;")) da_write(sys + "/vlc.desktop", da_app("VLC", "vlc %U", "video/mp4;audio/mpeg;", "AudioVideo;")) da_write(sys + "/evince.desktop", da_app("Document Viewer", "evince %U", "application/pdf;", "Office;Viewer;")) da_write(sys + "/evince-previewer.desktop", da_app("Print Preview", "evince-previewer %U", "application/pdf;", "Office;", extra="NoDisplay=true\n")) da_write(sys + "/htop.desktop", "[Desktop Entry]\nType=Application\nName=htop\nExec=htop\nTerminal=true\nCategories=System;Monitor;\n") // The system's cache says Chromium for mail links (it doesn't handle them: not installed for // that), Files for folders. da_write(sys + "/mimeinfo.cache", "[MIME Cache]\ninode/directory=org.gnome.Nautilus.desktop;\napplication/pdf=evince-previewer.desktop;evince.desktop;\n") // A system-wide default: VLC for video. da_write(root + "/etc/xdg/mimeapps.list", "[Default Applications]\nvideo/mp4=vlc.desktop\n") // The user's own file: Chromium for web pages, a removed viewer for PNGs, notes to keep. da_write( root + "/config/mimeapps.list", "# mine\n[Added Associations]\ntext/plain=org.gnome.gedit.desktop;\n\n[Default Applications]\ntext/html=chromium.desktop;\nx-scheme-handler/http=chromium.desktop;\nimage/png=gthumb.desktop;eog.desktop;\n\n[Removed Associations]\nx-scheme-handler/https=firefox.desktop;\n", ) da_write(root + "/config/xdg-terminals.list", "# preferred\nfoot.desktop\n") let dirs : @shell.XdgDirs = { config_home: root + "/config", config_dirs: [root + "/etc/xdg"], data_home: root + "/home-data", data_dirs: [root + "/usr/share"], desktops: ["chandra"], } (root, dirs) } ///| fn da_cat(id : String) -> Int { let mut n = -1 for i, c in @shell.app_categories { if c.id == id { n = i } } n } ///| fn da_names(ch : @shell.CategoryChoices) -> Array[String] { ch.apps.map(a => a.name) } ///| test "each category lists its installed apps and reads the current default" { let (_, dirs) = da_tree("read") let installed = @shell.scan_handler_apps(dirs) let ch = @shell.default_app_choices(dirs, installed, "") assert_eq(ch.length(), @shell.app_categories.length()) // Browsers: both, by name; the user's file says Chromium. let b = ch[da_cat("browser")] assert_eq(da_names(b), ["Chromium", "Firefox"]) assert_eq(b.current_id, "chromium") assert_eq(b.current, 0) assert_eq(b.missing, "") // Firefox handles PNGs and PDFs but is only a browser. assert_eq(da_names(ch[da_cat("images")]), ["Image Viewer"]) // PNGs: gThumb was chosen but is gone; the next entry, Image Viewer, is used. assert_eq(ch[da_cat("images")].current_id, "eog") assert_eq(ch[da_cat("images")].missing, "gthumb") // Mail: no default anywhere, the only app that handles it. assert_eq(ch[da_cat("email")].current_id, "thunderbird") // Folders: from mimeinfo.cache. assert_eq(ch[da_cat("files")].current_id, "org.gnome.Nautilus") // Video: the system-wide mimeapps.list in XDG_CONFIG_DIRS. assert_eq(ch[da_cat("video")].current_id, "vlc") assert_eq(da_names(ch[da_cat("music")]), ["VLC"]) // PDFs: the cache's first entry is hidden from menus but installed, so it is the default; // only the shown viewer is offered. assert_eq(ch[da_cat("documents")].current_id, "evince-previewer") assert_eq(ch[da_cat("documents")].current, -1) assert_eq(da_names(ch[da_cat("documents")]), ["Document Viewer"]) // Text: Added Associations. assert_eq(ch[da_cat("editor")].current_id, "org.gnome.gedit") // Terminal: xdg-terminals.list says foot. let t = ch[da_cat("terminal")] assert_eq(da_names(t), ["Foot", "kitty"]) assert_eq(t.current_id, "foot") // Nothing handles archives. assert_eq(ch[da_cat("archives")].apps.length(), 0) assert_eq(ch[da_cat("archives")].current_id, "") // The terminal chosen in state.edn wins; a chosen one that is gone is reported. assert_eq(@shell.current_terminal(dirs, installed, "kitty"), ("kitty", "")) assert_eq(@shell.current_terminal(dirs, installed, "alacritty"), ("foot", "alacritty")) } ///| test "setting a default writes mimeapps.list and keeps everything else" { let (root, dirs) = da_tree("write") // A desktop-specific file that names a browser would win over mimeapps.list: it is updated too. da_write(root + "/config/chandra-mimeapps.list", "[Default Applications]\ntext/html=chromium.desktop;\napplication/pdf=evince.desktop;\n") let comp = new_comp("da-write") comp.shell.xdg_dirs = dirs comp.shell.refresh_default_apps() let b = da_cat("browser") assert_eq(comp.shell.default_app_choices()[b].current_id, "chromium") assert_true(comp.shell.set_default_app(b, 1)) // Firefox assert_eq(comp.shell.default_app_choices()[b].current_id, "firefox") assert_eq( da_read(root + "/config/mimeapps.list"), "# mine\n[Added Associations]\ntext/plain=org.gnome.gedit.desktop;\n\n[Default Applications]\ntext/html=firefox.desktop;\nx-scheme-handler/http=firefox.desktop;\nimage/png=gthumb.desktop;eog.desktop;\nx-scheme-handler/https=firefox.desktop;\n\n[Removed Associations]\n", ) assert_eq( da_read(root + "/config/chandra-mimeapps.list"), "[Default Applications]\ntext/html=firefox.desktop;\napplication/pdf=evince.desktop;\nx-scheme-handler/http=firefox.desktop;\nx-scheme-handler/https=firefox.desktop;\n", ) // Picking the image viewer again clears the missing gThumb. let im = da_cat("images") assert_true(comp.shell.set_default_app(im, 0)) assert_eq(comp.shell.default_app_choices()[im].missing, "") assert_true(da_read(root + "/config/mimeapps.list").contains("image/png=eog.desktop;\nx-scheme-handler/https=firefox.desktop;\nimage/jpeg=eog.desktop;\n")) // Out of range: nothing happens. assert_false(comp.shell.set_default_app(im, 5)) assert_false(comp.shell.set_default_app(99, 0)) } ///| test "the terminal choice runs terminal apps and goes first in xdg-terminals.list" { let (root, dirs) = da_tree("term") let comp = new_comp("da-term") comp.shell.xdg_dirs = dirs comp.shell.refresh_default_apps() assert_eq(comp.shell.in_terminal("htop"), "foot -e htop") let t = da_cat("terminal") assert_true(comp.shell.set_default_app(t, 1)) // kitty assert_eq(comp.shell.terminal, "kitty") assert_eq(comp.shell.in_terminal("htop"), "kitty htop") assert_eq(da_read(root + "/config/xdg-terminals.list"), "kitty.desktop\n# preferred\nfoot.desktop\n") assert_true(comp.shell.set_default_app(t, 0)) // foot assert_eq(da_read(root + "/config/xdg-terminals.list"), "foot.desktop\nkitty.desktop\n# preferred\n") } ///| test "the settings module's Apps tab shows each kind's default and sets it" { let (root, dirs) = da_tree("tab") let comp = new_comp("da-tab") comp.shell.xdg_dirs = dirs let slot = comp.shell.slot_of("session-settings") assert_true(comp.shell.open_settings(Apps)) ignore(comp.compose_frame()) // Ten kinds, plus the line saying gThumb is gone. let kinds = @shell.app_categories.length() let pr = comp.shell.slot_popup_rect(slot) assert_eq(pr.h, 40 + (kinds + 1) * 28 + 6) let texts = comp.shell.slot_popup_texts(slot).map(t => t.0) assert_true(texts.contains("Web browser")) assert_true(texts.contains("Chromium")) assert_true(texts.contains("! gthumb is not installed any more")) assert_true(texts.contains("None installed")) // Row i's middle (the popup grows upwards from the panel, so read where it is now). let row_y = fn(i : Int) { comp.shell.slot_popup_rect(slot).y + 40 + i * 28 + 13 } // Click "Web browser": its two apps are listed under it. ignore(@sys.write_file_bytes("_build/default_apps_tab.png", @renderer.encode_png(comp.compose_frame()))) click_at(comp, pr.x + 40, row_y(0), 5U) assert_eq(comp.shell.slot_popup_rect(slot).h, pr.h + 2 * 28) ignore(@sys.write_file_bytes("_build/default_apps_open.png", @renderer.encode_png(comp.compose_frame()))) assert_true(comp.shell.slot_popup_texts(slot).map(t => t.0).contains("Firefox")) // Click Firefox: it is the browser now, and the list folds up. click_at(comp, pr.x + 60, row_y(2), 10U) ignore(comp.compose_frame()) assert_eq(comp.shell.default_app_choices()[da_cat("browser")].current_id, "firefox") assert_true(da_read(root + "/config/mimeapps.list").contains("text/html=firefox.desktop;")) assert_eq(comp.shell.slot_popup_rect(slot).h, pr.h) assert_true(comp.shell.slot_popup_texts(slot).map(t => t.0).contains("Firefox")) // A kind with nothing installed doesn't open. let arch = da_cat("archives") + 1 // under the gThumb line click_at(comp, pr.x + 40, row_y(arch), 20U) assert_eq(comp.shell.slot_popup_rect(slot).h, pr.h) } ///| test "the chosen terminal is saved to state.edn and restored" { if @sys.getenv("CHANDRA_TEST_CONFIG") == "1" { write_state_edn("{:note \"keep\"}\n") let (_, dirs) = da_tree("save") let a = new_comp("da-save-a") a.shell.persist = true a.restore_state() a.shell.xdg_dirs = dirs a.shell.refresh_default_apps() assert_true(a.shell.set_default_app(da_cat("terminal"), 1)) let text = read_state_edn() assert_true(text.contains(":terminal \"kitty\"")) assert_true(text.contains(":note \"keep\"")) let b = new_comp("da-save-b") b.shell.persist = true b.shell.xdg_dirs = dirs b.restore_state() assert_eq(b.shell.terminal, "kitty") assert_eq(b.shell.default_app_choices()[da_cat("terminal")].current_id, "kitty") } }