Fixing QtKeychain freezing on Apple devices

Working on the Nextcloud desktop client, I stumbled into a nasty little bug. If you tried to add or remove a user account, the app would often freeze for a while.

This was super annoying during testing, so I decided to look into it. Surprisingly this wasn’t a new bug I’d unwittingly introduced; tracing the source of the freeze led all the way down into QtKeychain, which is what the desktop client uses to store user credentials.

QtKeychain, like Qt itself, is great at making life easy for developers targeting multiple platforms as it abstracts away the different keychain/keyring/wallet implementations for the different platforms. Looking at the Objective-C++ code handling Apple platforms, QtKeychain’s different job classes called code using Keychain Services functions, as expected.

Fortunately the problem was pretty clear. All of the calls to the Keychain Services functions — SecItemUpdate, SecItemDelete, SecItemAdd, SecItemCopyMatching — block the calling thread. From Apple’s documentation:

SecItemUpdate blocks the calling thread, so it can cause your app’s UI to hang if called from the main thread. Instead, call SecItemUpdate from a background dispatch queue or async function.

This means that, when called on the main thread, doing any QtKeychain operations would lead to freezing the UI. Since this is not a problem on other platforms (that I know of!), it’s pretty easy to overlook this quirk of Keychain Services’ API. 

void DeletePasswordJobPrivate::scheduledStart()
{
    const NSDictionary *const query = @{
            (__bridge id) kSecClass: (__bridge id) kSecClassGenericPassword,
            (__bridge id) kSecAttrService: (__bridge NSString *) service.toCFString(),
            (__bridge id) kSecAttrAccount: (__bridge NSString *) key.toCFString(),
    };

    const OSStatus status = SecItemDelete((__bridge CFDictionaryRef) query);

    if (status == errSecSuccess) {
        q->emitFinished();
    } else {
        const ErrorDescription error = ErrorDescription::fromStatus(status);
        q->emitFinishedWithError(error.code, Job::tr("Could not remove private key from keystore: %1").arg(error.message));
    }
}

With use of Grand Central Dispatch, a fix was relatively straightforward. All that needed to be done was to call the offending functions from a different dispatch queue. I rewrote the Apple-specific keychain implementation in order to call the Keychain Services functions asynchronously from the global queue, passing an interface object that would call the relevant completion methods and signals on the read/write/delete QtKeychain jobs.

static void StartDeletePassword(const QString &service, const QString &key, AppleKeychainInterface * const interface)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSDictionary * const query = @{
            (__bridge NSString *)kSecClass: (__bridge NSString *)kSecClassGenericPassword,
            (__bridge NSString *)kSecAttrService: service.toNSString(),
            (__bridge NSString *)kSecAttrAccount: key.toNSString(),
        };

        const OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);

        if (status == errSecSuccess) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [interface keychainTaskFinished];
                [interface release];
            });
        } else {
            NSString * const descriptiveErrorString = @"Could not remove private key from keystore";
            dispatch_async(dispatch_get_main_queue(), ^{
                [interface keychainTaskFinishedWithError:status descriptiveMessage:descriptiveErrorString];
                [interface release];
            });
        }
    });
}

Note that after these changes, your application will need to either use QGuiApplication or you will need to initialise NSApplication yourself if you use QCoreApplication.

Testing the changes with a fresh build of QtKeychain, I was happy to see the freezes are now gone in the Nextcloud desktop client. 🙂

No more freezing!

Thanks to Frank Osterfeld for reviewing the pull request!

KDE PIM in July and August

KDE PIM is the set of applications that helps you manage your email, contacts, appointments, tasks and more.

Since our last report covering KDE PIM in May and June, the PIM applications and libraries have seen over 1200 changes from almost 30 contributors. Let’s go over some of the biggest updates.

New features

Kalendar

I have been working on improvements to the calendar side of Kalendar. New changes include the addition of new and improved differentiation between all-day and not all-day events in the month view. This change makes the month view look quite different — in a good way!

Another new addition is the inclusion of a new incidence information popup in some of the calendar views. In views where horizontal space is at a premium, such as the month view or the week view, the popup lets us present the same amount of information without squishing the contents of the calendar view.

Of course, this change is optional, and can be toggled in the settings if you choose to do so.

Carl has worked hard on polishing Kalendar’s new address book feature. New additions include a context menu for contacts in the contact list containing commonly-used actions, as well as more contact management features that should better help you manage the people in your life.

We have both also worked hard on improving the overall stability of Kalendar. Lots of bugs and crashes have been fixed in the application and the common reminder daemon used by Kalendar and KOrganizer. Specifics can be found below in the bug fixes section.

During the Promo sprint in Saumur (France), Carl and I met and discussed Kalendar at length. We have big, ongoing plans for the future, which we will be discussing further at Akademy 2022!

KDE Itinerary

Volker has focused on improving Itinerary’s calendar integration. New features include supporting import and export with Akonadi calendars, and recognizing more travel-related calendar content automatically. These new features should make planning your next trip much easier.

For details see Volker’s dedicated summary post.

Calendar import page recognizing transport elements and allowing individual selection.

Kleopatra

Usability and accessibility was a main focus of development for Kleopatra in the last two months. Specifically:

Moreover, some new features were added:

KMail

Laurent has merged improvements to KMail’s translator support, which now uses a new translation engine. This should make translations of emails in unfamiliar languages more accurate than ever.

Another new addition is the inclusion of an embedded mail delivery notification widget in the email viewer, which should make interacting with this feature feel better integrated and easier to deal with.

You can now interact with mail delivery notification-related actions directly from the email viewer

Lastly, KMail now has a new plugin allowing you to manage the Akonadi database within KMail.

KOrganizer

Glen has worked on KOrganizer’s drag-and drop capabilities. The month view now lets users drag events, todos, and more out of KOrganizer. These items can then be dropped into other places, such as KMail’s email composer window, where they can be used to send event information.

This feature is now available in KOrganizer’s Month, Agenda, and Todo views.

Bug fixes and compatibility improvements

Each of the PIM apps have also had many bug fixes that should make them more stable and usable. Additionally, we have also been hard at working preparing for Qt6 and making changes to ensure compatibility.

KAlarm

Kalendar

Kalendar/KOrganizer reminder service

Kleopatra

KMail

Fix Bug 456578 recipient auto-completion does not work in master git snapshots
Fix bug 448674: Spam false-positive, because link’s href & text capitalization 
mismatch
Fix 395711: External images not loaded by default if message was opened in a 
separate window

Kontact

Fix bug 457241: When the user uses the `New To-do…` menu item, the to-do is created in the system time zone instead of the “floating” time zone ().

Help us make Kontact even better!

Check out some of our open junior jobs! They are simple, mostly programming-focused tasks, but they don’t require any deep knowledge or understanding of Kontact, so anyone can work on them. Feel free to pick any task from the list, then get in touch with us! We’ll be happy to guide you and answer all your questions. Read more here…

Kalendar 1.0 is out!

You might have noticed that there have been a lot of KDE releases this week, like Plasma 5.24 and Plasma Mobile Gear. Keen to hop onto this train, those of us in the Kalendar team are happy to reveal Kalendar version 1.0.

We are even happier to announce that this will be the final version of Kalendar before we will hopefully become part of KDE Gear, where Kalendar will join the rest of the KDE Application family in a 4 month release schedule, with bug fix releases in between.

A massive thank you, and much love, to everyone who has helped Kalendar reach this point! ❤️

Feel free to join us in Kalendar’s Matrix room., where we discuss Kalendar’s development, direction, and more. We are always looking for new contributors!

Our 1.0 release

We have worked hard over the past year to bring you a calendar app that you will enjoy using and that will fulfill your calendaring needs. We hope you enjoy using it as much as we have enjoyed making it. 🙂

It is now in the hands of distribution packagers to add Kalendar to their repositories. The most up-to-date and unstable version of Kalendar will continue to come from our git repository, and some users have gone ahead and started packaging builds of Kalendar coming straight from our master branch.

Git builds:

Stable versions are now also available on a number of distributions, such as Alpine, Fedora, Neon User, Manjaro, and NixOS!

Kalendar will obviously still get new features, additions, and changes in line with your feedback, our ideas, and the direction in which the KDE community as a whole moves towards. With 1.0, we think we have a good application that is complete enough and stable enough for most people to use at home.

With that, lets go through what has changed since our previous release!

Massively improved stability, reliability, and predictability

A significant amount of effort has been undertaken over the past month to address Kalendar’s major bugs and to make the application as stable and easy to live with as possible. While this is not a glamorous new feature, it is the most significant reason why this release is 1.0 — we now trust it to not crash under most use-cases, and we have stamped out all critical bugs that we have found and been made aware of. You can find a full list of bug-fixes and other smaller or backend changes at the end of this post.

That’s not to say we don’t have any exciting changes…

Vastly improved event and task reminder notifications

Thanks to Volker’s hard work, our event and task reminder notifications have been vastly improved since our last release.

Notifications now display event and task information much more clearly and concisely.

For example, we display different text depending on whether an incidence is a task or an event. An event reminder notification will display the start time of the event and the minutes left until the beginning of said event, letting you know how long you have until you need to be at that meeting. A task reminder, meanwhile, will let you know when that task is due, and tell you how many minutes you have left until you need to hand in that assignment.

If an incidence has a URL attached to its location information, the reminder notification will provide a button with which you can quickly open your default web browser. If you happen to have geolocation data attached to your event or task, this button will open your default mapping application instead.

You can now also click on the notification itself and open the event or task inside Kalendar. Upon clicking on the notification, Kalendar will pop up with the view set to the incidence time and with the incidence opened for viewing.

Work is now well underway for our notification system to make its way into KOrganizer too, so those of you who use that application will be able to enjoy all of these changes in the coming releases!

Splashes of colour

Calendar colours are one of the quickest ways to be able to differentiate one calendar from the next, and we’ve added touches of these colours throughout the application.

First, the incidence editor’s calendar selector now has a coloured circle that matches the selected calendar. If you’re like me and you can only really tell what calendars you’re using by colour, that should be helpful! You can also find these in the tasks view’s calendar picker, which you can invoke by adding a quick task in the ‘All tasks’ mode.

A smaller touch has been added to the circular checkboxes in the tasks view. Tasks’ checkboxes now have a subtle background colour matching the colour of the checkbox outline and fill, which makes them look quite nice.

Usability improvements

Thanks to Slawek, Kalendar is now smarter about what start times and due times are set by default for an incidence. New events will always have start times set to the nearest future quarter-hour, or due times in the case of tasks.

Slawek has also now enabled the option for incidences in the week, three-day, and day views to be dragged between the all-day and hourly sections, allowing you to quickly change an event from taking up the entire day to only taking up a section of said day. These views have also gained a nice animation when clicking the “Now” button, which scrolls the view down to the current time.

Additionally, it is now much easier to distinguish between incidence types in the month view. We have replaced redundant icons for incidence types with more useful start times for events, retaining only the type icons for tasks to make them easy to differentiate from events. Thanks to this, it is now also easier to differentiate between all-day and non all-day event types just by looking at whether or not they have a start time in the incidence label.

Tasks in the tasks view will now also display how complete they are in the circular checkboxes, by filling in the rough completion percentage. This lets you see at a glance just how much stuff you have left to do!

Those of you with calendar sources using calendar folders that can themselves have incidences in them are now able to customise these folders as you wish by right-clicking on them in the sidebar, much like other calendars. Additionally, this feature has been extended to calendar sources themselves, and you can now customise their names and icons, refresh them, or delete them from the sidebar.

image

Finally, if you have your desktop set to a language that is written right-to-left such as Arabic, Kalendar will be more usable for you as we have ensured that components such as the tasks view’s tree now correctly adapts to a right-to-left layout.

Bug-fixes, back-end and small changes

Sooooo many.

Supporting us

Is there anything you’d like to see added to Kalendar? Get in touch! I’m @clau-cambra:kde.org on Matrix.

If you want to support Kalendar’s development, I strongly encourage you to donate to the KDE community. These donations help us keep our infrastructure running, including our GitLab instance, our websites, and more. You can donate at https://kde.org/community/donations/.