Open file with default application from Vala? - vala

What's the best way to open a file in the default application from Vala?
A bit like how xdg-open works.

I found some existing code in another application, but later on I also found this
GLib.AppInfo.launch_default_for_uri method.
A simple example:
var file = File.new_for_path (file_path);
if (file.query_exists ()) {
try {
AppInfo.launch_default_for_uri (file.get_uri (), null);
} catch (Error e) {
warning ("Unable to launch %s", file_path);
}
}

If you're using GTK, then you've also got Gtk.gtk_show_uri_on_window(), which uses the GLib stuff under the hood.

As far as I know there is only one implementation of the relevant freedesktop.org standards.
That is the reference implementation in xdg-utils:
https://www.freedesktop.org/wiki/Software/xdg-utils/
The tools are written in shell script, for example here is the source code for xdg-open:
https://cgit.freedesktop.org/xdg/xdg-utils/tree/scripts/xdg-open.in
So by far the easiest way is to just call the xdg-open script via Process.spawn_async and friends.
If you insist on using a library function you would have to implement a standard conforming library yourself.
Update:
There are quite a few libraries in various languages that implement some of the freedesktop.org standards, for example here is a list on GitHub:
https://github.com/topics/xdg
For example here is a similar tool to xdg-open written in D:
https://github.com/FreeSlave/mimeapps/blob/master/source/mimeapps.d
What I didn't find so far is a Vala / GLib or plain C library that could easily be used from a Vala application.
Update 2:
Actually it turns out there is something for that purpose in GLib (or more precisely in Gio):
https://valadoc.org/gio-2.0/GLib.AppInfo.launch_default_for_uri_async.html
https://developer.gnome.org/gio/stable/GAppInfo.html
So you should be able to use the GLib.AppInfo.launch_default_for_uri_async method.

Related

is it possible to call require from C

I have a module compiled in a shared object (I followed the library part of this article https://chsasank.github.io/lua-c-wrapping.html) and I want to load it from C not from the interpreter.
Is it possible ? If so how to do it ?
Yes, it's possible, as require is a function stored in a global environment. Lua does the same in standalone interpreter when it needs to process the -l option, see the dolibrary function.
You do this the same way as with any other global function - in simplest case calling lua_getglobal(), then pushing the name of the file to require, and calling lua_call/lua_pcall/whatever.
I know that I am late, but someone else may struggle with this right now (like I just did).
This is a simple way of doing "require" from C:
int reqRes = luaL_dostring(L, "local t=require('myLib') return (t~=nil)");
if (reqRes==0)
//success
else
//failed
Unfortunately, right now, I'm using Lua 5.1 and "dolibrary" function doesn't exist, I >tried to take some part of the code and it crashes :\ So, for now, I use luaL_dostring(L, >"require 'libMyWrappings'"); libMyWrappings must be in the same directory as the c >program, and I can't use a path to indicate the lib. – Aminos Jan 22 at 11:45
I just ran into the same issue, it has to do when the package library is loaded
{LUA_LOADLIBNAME, luaopen_package}
needs to happen before you try and call it

How do I create a global key-binding in a gnome-shell extension

I'm running Ubuntu Gnome, gnome shell version 3.24.2. I'm trying to create a gnome shell extension, and am using Javascript.
In my extension and in Javascript, how do I create a global key binding that is recognized from anywhere in Gnome? I want to bind a function to that key binding so that when the user presses the key combination then the said function is executed.
There's virtually no documentation on gnome-shell extension development. So asking here is my only option. Please don't refer me to the following stackoverflow question, since its answer is for Gnome 3.22 and I sincerely hope there's an easier way to create a key-binding: Gnome Shell Extension Key Binding
There a pretty complete answer here: How to handle keyboard events in gnome shell extensions?
You may find the easiest way is to use Gnome Shell's DBus interface, but dealing with shortcut conflicts and handling the signal callbacks is unavoidable:
Bus Name: org.gnome.Shell -> Path: /org/gnome/Shell -> Interface:
org.gnome.Shell
Relevant Methods:
GrabAccelerator(String accelerator, UInt32 flags) -> (UInt32 action)
UngrabAccelerator(UInt32 action) -> (Boolean success)
Signal:
AcceleratorActivate(UInt32, Dict of {String, Variant})

How can you get information about other apps running or in focus?

My motivation: I'm writing an app to help with some quantified self / time tracking type things. I'd like to use electron to record information about which app I am currently using.
Is there a way to get information about other apps in Electron? Can you at least pull information about another app that currently has focus? For instance, if the user is browsing a webpage in Chrome, it would be great to know that A) they're using chrome and B) the title of the webpage they're viewing.
During my research I found this question:
Which app has the focus when a global shortcut is triggered
It looks like the author there is using the nodObjc library to get this information on OSX. In addition to any approaches others are using to solve this problem, I'm particularly curious if electron itself has any way of exposing this information without resorting to outside libraries.
In a limited way, yes, you can get some of this information using the electron's desktopCapturer.getSources() method.
This will not get every program running on the machine. This will only get whatever chromium deems to be a video capturable source. This generally equates to anything that is an active program that has a GUI window (e.g., on the task bar on windows).
desktopCapturer.getSources({
types: ['window', 'screen']
}, (error, sources) => {
if (error) throw error
for (let i = 0; i < sources.length; ++i) {
log(sources[i]);
}
});
No, Electron doesn't provide an API to obtain information about other apps. You'll need to access the native platform APIs directly to obtain that information. For example Tockler seems to do so via shell scripts, though personally I prefer accessing native APIs directly via native Node addons/modules or node-ffi-napi.
2022 answer
Andy Baird's answer is definitely the better native Electron approach though that syntax is outdated or incomplete. Here's a complete working code snippet, assumes running from the renderer using the remote module in a recent Electron version (13+):
require('#electron/remote').desktopCapturer.getSources({
types: ['window', 'screen']
}).then(sources => {
for (const thisSource of sources) {
console.log(thisSource.name);
}
});
The other answers here are for the rendering side - it might be helpful to do this in the main process:
const { desktopCapturer } = require('electron')
desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
for (const source of sources) {
console.log("Window: ", source.id, source.name);
}
})

API implementation changes for OpenCV 3.0 instead of OpenCV 2.4

I'm trying to update from OpenCV2.4.10 to OpenCV3.0.0. OpenCV provides a basic guide for how general APIs work after the update, however, it doesn't cover how to deal with some implementation and API changes that have been removed completely. Is there a definitive guide to this process?
For example, one specific problem I'm encountering is that a library I'm using calls a function that has been removed from OpenCV3:
static CV_IMPLEMENT_QSORT( icvSortDistances, int, CV_LT )
How can I replace CV_IMPLEMENT_QSORT? It appears to be originally defined in cxtypes according to this blog post. There is another similar function of note CV_DECLARE_QSORT, which may also not be in 3.0?
Also several components use the opencv legacy libraries, are there any known or suggested upgrade paths for those?
How about replacing it with :
static void icvSortDistances(int *array, size_t total, int )
{
std::sort(&array[0], &array[total]);
}

How to force generating a warning in DartEditor

I'm looking for a way to generate a warning anywhere in my code (top level, class, functions). As I'm typically having a 0 warning policy, this enables me to see where I make a change I need to revert before commit
For example in Java, i could do this:
private int warning_revert_to_false;
boolean DEBUG = true;
and it will generate a warning (according to my settings).
Using jslint/jshint that's easy (mixed tab/space for example), in C/C++ i can use pragma...
Basically I want the code to still compile and run and so far in Dart, I could not find a simple solution and I'm sure there is one that I have missed. Thanks!
Add the following library to your program:
library forced;
import 'package:meta/meta.dart';
class Forced {
#deprecated
static void warning() {
}
}
Wherever you want a forced warning you can simply write:
Forced.warning();

Resources