ia: Benvenite! In mi blog io scribe in interlingua, italiano e anglese.

it: Benvenuti! Nel mio blog scrivo in interlingua, italiano e inglese.

en: Welcome! In my blog I write in Interlingua, Italian and English.

Crescer, o abandonar Facebook

Io ha finalmente decidite de cancellar mi conto de Facebook. Isto es un decision que io initiava a considerar jam alicun annos retro, ma al qual io usque ora ha preferite lo de limitar maximemente mi usage del sito: io lo utilisa solmente in modalitate de sol scriptura, i.e. solmente pro diffunder propaganda politic e, generalmente, pro render lo un loco horribile anque pro tote le alteres. Mi rationamento esseva iste: si io esseva un persona famose, alora io abandonarea le sito sin hesitation, con grande ruito e impacto super les qui me seque; ma post que io es connesse solmente con pauc personas, le unic maniera pro haber un impacto (ben que minime) es lo de remaner e “salvar” mi amicos, convincer lo super mi opiniones e/o incoragiar les con mi comportamento a abandonar le sito.

Il existe vita anque foras de Facebook

Totevia, plus le tempore passava e plus io initiava a dubitar super le bontate de iste idea. Mi objectivo, in fundo, es que le personas lassa Facebook; ma si tanto que io remane in Facebook como totes, mesmo si protestante e destructive, io es comocunque parte del business de Facebook e non presenta in ulle maniera un defia al status quo. Il non ha alicun possibilitate de convincer alicun a migrar a plattaformas alternative (o a abandonar le retes social del toto) durante que nos mesme continua a esser in illo. In ultra, io sape anque troppo ben que il es practicamente impossibile convincer un persona super proprie ideas in un rete social; e anque mi plano perverse de render horribile iste loco pro totes non pote functionar: mesmo si uno perdeva su patientia, ille simplemente me bloccarea e continuarea como antea.

Del altere latere, per abandonar definitivemente Facebook il ha certemente le possibilitate que necuno se curara de isto, ma al minus io lo ha lassate. Io non plus essera un resursa pro Facebook. In le grandor del schemas, isto es multo poco, un parve gutta in le oceano, ma io crede que isto es le resolution melior. Esque io lo regrettara? Personalmente, non del toto: io ha cessate de consumar Facebook jam desde un par de annos. Certemente, il es possibile que le numero de visitas a mi blog e a mi canal in Youtube (io parlara de iste altere monstro un altere vice) diminuera; in nostre tempore, il non ha multe personas qui sape como utilisar un signalibro in lor navigator (pro non parlar del fluxos RSS) ma, honestemente, il non ha multo que io pote facer pro remediar al situation.

Le gigantes technologic del interrete rende nostre vita plus facile, sin dubita; il ha devenite plus facile tener se in contacto, trovar informationes, consolidar un opinion mainstream. Toto es talmente conveniente, que illo nos face non solmente plus pigre, ma anque plus incapace, al puncto que sin lor servicios nos nos sentirea perdite e sin resursas. Il non plus se tracta solmente de un dependentia psychologic: illos deveni un necessitate practic. Isto, in torno, nos rende debile, facile a manipular e, ultimemente, sclavos.

Io crede in le interrete como un inter rete: un loco distribuite, interconnesse, sin padron. Mi providitor de spatio web decide de censurar me e clauder mi sito? Ben, io aperi un altere sito con un altere providitor. Ma si Facebook te claude le accesso, immediatemente tu perde le connessiones a tu amicos, al gruppos in le qual tu participava, le mercato, le eventos local, le novas del die, tu profilo, tu historia.

E io, io vole viver in un mundo ubi io es le padron de mi vita.

How to be an awesome member of the Free Software community

No, this is not a tutorial, unfortunately. The main reason being that, while I strive to do my best, I don't consider myself to be an excellent member of the Free Software community, let alone be able to teach others about it. But this morning I got an email from the FSF about a campaign for St. Valentine's day, which reminded me of something I've been planning to do since a long time ago, but never got to it.

Love

I want to publicly send huge thanks to Robin Mills from the Exiv2 project, and not only because I've been fruitfully using his work in three (!) projects of mine (PhotoTeleport, Mappero Geotagger and Imaginario), but also, and especially, for being an extremely pleasant interlocutor. On the web, yes. Whereas most people tend to be more thorny and touchy in their interactions over the internet, Robin has always been friendly and coversational, trying to form a bond with some complete foreigner who just happened to report a bug on Exiv2.

Just adding some bits of information about one's personal events (such as travels) can make an enormous difference on how one's attitude is perceived. Mentioning that you visited the place that is familiar to the bug reporter almost makes one forget of being sitting in front of a computer on the internet, because your mind flies to that place. Considering that even on this personal blog of mine I'm kind of reticent about speaking of my private life, I cannot help appreciating the friendly attitude that Robin reserves for people writing on a bug tracker.

You are a wonderful netizen, Robin. A happy Valentine Day to you and your family. Thank you.

Invoking a C++ function from QML, asynchronously

In the Imaginario code I had written a C++ method to find all files present in a directory tree (for some reason this code is always encoding file paths as QUrl, but feel free to ignore that):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
QList<QUrl> Utils::findFiles(const QUrl &dirUrl, bool recursive) const
{
    QList<QUrl> files;

    QDir dir(dirUrl.toLocalFile());
    if (Q_UNLIKELY(!dir.exists())) return files;

    auto list = dir.entryInfoList(QDir::NoDotAndDotDot |
                                  QDir::Dirs | QDir::Files,
                                  QDir::Name);
    for (const QFileInfo &info: list) {
        if (info.isDir()) {
            if (recursive) {
                files += findFiles(QUrl::fromLocalFile(info.filePath()), true);
            }
        } else {
            files.append(QUrl::fromLocalFile(info.filePath()));
        }
    }

    return files;
}

The Utils mentioned in this snipped is a QObject-derived class which is registered to QML as

1
2
3
4
5
6
7
8
static QObject *utilsProvider(QQmlEngine *, QJSEngine *)                                               
{                                                                                                      
    return new Utils;                                                                                  
}

...
// In some other part of the code, before entering the main loop:
qmlRegisterSingletonType<Utils>("Imaginario", 1, 0, "Utils", utilsProvider);

This allows me to call the C++ findFiles() method from QML, like this:

1
2
3
4
import Imaginario 1.0

...
    onClicked: importer.addFiles(Utils.findFiles(folder, true))

So far, so good. However, I couldn't help noticing that when the selected folder contains a large number of files, the whole UI freezes until the findUtils() method has returned. So, how can I invoke my C++ method without blocking the UI?

QML offers a WorkerScript element which seems to do exactly what we need, but unfortunately the possibility of invoking C++ code is only there since Qt 5.12 (before that version, worker script could not use import statements), and in any case the requirement to store the script into a separate Javascript file makes the code less readable.

A better option, in my opinion, is to write a C++ method that performs the lengthy task in a thread and invokes a Javascript callback once the job execution is completed. This sounds pretty complex at first, but fortunately Qt's nice APIs make this an almost trivial task. Without modifying our previous findFiles() method, we write a second one which will execute it in a thread, using QtConcurrent::run():

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
void Utils::findFiles(const QUrl &dirUrl,
                      bool recursive,
                      const QJSValue &callback) const
{
    auto *watcher = new QFutureWatcher<QList<QUrl>>(this);
    QObject::connect(watcher, &QFutureWatcher<QList<QUrl>>::finished,
                     this, [this,watcher,callback]() {
        QList<QUrl> files = watcher->result();
        QJSValue cbCopy(callback); // needed as callback is captured as const
        QJSEngine *engine = qjsEngine(this);
        cbCopy.call(QJSValueList { engine->toScriptValue(files) });
        watcher->deleteLater();
    }); 
    watcher->setFuture(QtConcurrent::run(this, &Utils::findFiles, 
                                         dirUrl, recursive));
}

You might be surprised, but this is all what is needed in the C++ side. The changes to the QML side are equally trivial:

1
2
3
4
5
6
7
8
import Imaginario 1.0

...
    onClicked: {
        Utils.findFiles(folder, true, function(files) {
            importer.addFiles(files)
        })
    }

That's it! Line 6 in this last snippet will be invoked once the findFiles() method has completed its execution, and you'll be glad to see that the UI will be responsive throughout the duration of the operation.

QtConcurrent alternatives

QtConcurrent::run() is very simple to use, but it's not without its shortcomings, the biggest of which is that it doesn't support cancelling. This is something I can live with in this particular case, but, if you can't, don't despair: there are other options. The second simplest one is probably the static QThread::create() method, which behaves similarly to QtConcurrent::run() but returns a QThread object which can be requested to terminate, for example with QThread::requestInterruption(). This method exists only since Qt 5.10, and that's why I didn't list it as my first choice; but if you are using such a recent version of Qt, then it's probably the best option, because one doesn't even need to subclass QThread (and there's no need to depend on QtConcurrent). Of course, subclassing QThread is also an option, but this involves a little more typing.

Beware of threading issues

The most tempting solution (and I confess, my first approach to the problem was exactly this) is to invoke the Javascript callback directly from the thread:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
void Utils::findFiles(const QUrl &dirUrl,
                      bool recursive,
                      const QJSValue &callback) const
{
    QtConcurrent::run([=]() {
        const QList<QUrl> files = findFiles(dirUrl, recursive);
        QJSValue cbCopy(callback);
        QJSEngine *engine = qjsEngine(this);
        cbCopy.call(QJSValueList { engine->toScriptValue(files) });
    });
}

However, this will work 95% of the times only, because QJSValue::call() is not thread-safe, and therefor your application will be victim of random crashes. So, we need to write a couple of lines more and invoke the callback from the main thread, like I'm doing before.