Getting QT to respond to power-events (sleep/hibernate) - c++

I'm trying to develop software that is intelligent wrt sleep events (cleanly closing network connections, making sure data restart locations are set properly, etc).
Are there mechanisms in QT (4.6) currently that facilitate me responding to system power events?

I dont think there's a native to qt and multi platform way for these but there are propably some ways and api's to do things you are looking for. Posix signals might provide something to notify your app about ongoing system events. Also you might want to look QDbus stuff, some cases dbus will broadcast system events.. But how to do the stuff you are really looking for is very os dependant and without knowning your target os, its rather hard to point out a optimal solution.

No, nothing like that in Qt no. Probably not and will not.
You need to use the API of the operating system.

Related

Qt Inter-Process function trigger

I know I can use QSharedMemory to transfer information between applications (I've used it successfully). But I'd like to trigger a function on another application. Ex: App1 has a button that when pressed triggers a function on App2 that prints "HelloWorld".
I've checked IPC solutions' page and I see that linux supports D-Bus communications and Qt uses it to extend signals and slots to IPC level, which is essentially the kind of thing I want. But I'm working on Windows for now and need a solution that is compatible with both OSs.
I also see that perhaps I could use TCP/IP solutions in Qt library to establish a server/client communication, but given that I'm not so versed in it I'd prefer something simpler. But maybe TCP/IP is quite simple and with a few variables and functions calls I could get it done. Please tell me if that's the case.
I've also thought of using a boolean variable on QSharedMemory that is set by the master app and is regularly checked and cleared by the slave app when a timer, running on a loop, runs out. But that means more overhead. It's not at all a clean solution, but it would work ok in my case.

which c/c++ library can be used for handling wifi connections for linux?

I want to implement WiFi manager program which should handle the following.
notification on wi-fi access point has come or gone
provide information of available wifi access point
connect/disconnect with a given wi-fi access point
Which is the recommended C/C++ WiFi library for Linux to achieve this?
On Fedora (at least), the preferred way to interact with NetworkManager is via DBus.
While wireless-tools and the like will work — even direct kernel calls, if you must — there are a couple of problems:
You'll probably need superuser privileges
NetworkManager will probably have a panic attack and get into fights with you, unless you stop its service
The user's normal networking controls (e.g. desktop tray icons) are almost certainly configured to use NetworkManager.
You can send and receive DBus messages for all the tasks you mentioned, for WiFi as well as arbitrary other types of network interfaces. The API is published here, for version 0.8.
For newer operating systems, there are apparently changes in the API, with a migration guide.
Their wiki should be really helpful.
I know both Fedora and Ubuntu use NetworkManager by default; I believe many other systems do, as well, but don't have an exhaustive list.
Of course, if you're using an embedded system, custom distribution, or something, then your mileage may vary.
That would be wireless-tools
I would recommend using directly the NetworkManager Library.
You can use low-level D-Bus library or libnm-glib library, that makes communication easier: example add connection glib
For more info, you can take a look into the code of the command line client nmcli.

Detecting keypresses when application has no focus

I have an application that lives in the tray, and I need to detect the user activity so that I can perform certain tasks. For windows, I know that I can use GetAsyncKeyState or SetWindowsHookEx, but I would like to have a cross-platform solution.
Does anyone have a solution for this? maybe a reimplementation of keyPressEvent? Or would it be easier to just setup some macros and do it separately?
This is inherently platform specific.
I'd make sure to isolate whatever you do and separate the mechanism of capturing the event and recording the keystrokes from the consumption of those keystrokes. That way you only have to implement the capture/record mechanism on each platform and all of the other code should be pretty portable.
You may want to take a look at what SDL has done here. http://www.libsdl.org/
They have managed to abstract off all of that sort of thing, not just keyboards.

Register to the DeviceManager of Linux

I read some questions here but couldn't really find the specific problem I'am faced with here...
I need to implement a "DeviceCache" in a particular project which caches all device-names found in /proc/net/dev .
The Language is C/++
So I thought about a seperate thread looking every X seconds in the directory mentioned above but was encouraged to find a more direct way.
How can I register a method of my process to the device manager of linux?
Is there a similar way like events/signals?
I looked in other sites but couldn't find any helpful code... Im relatively new to Linux-programming but willing to learn new things :)
Based on your comments, what you really want is to track which network interfaces are operational at any given time.
The only true way to determine if a network interface is up is to test it - after all, the router on the other end may be down. You could send pings out periodically, for example.
However, if you just want to know if the media goes down (ie, the network cable is unplugged), take a look at these SO questions:
Linux carrier detection notification
Get notified about network interface change on Linux
If you just want to be notified of the actual hardware-level registration of interfaces (eg, when a USB NIC is plugged in), you can use udev events if your platform has udev; otherwise, I believe there's another netlink category for hardware addition/removal events.

How to be notified of file/directory change in C/C++, ideally using POSIX

The subject says it all - normally easy and cross platform way is to poll, intelligently. But every OS has some means to notify without polling. Is it possible in a reasonably cross platform way? (I only really care about Windows and Linux, but I use mac, so I thought posix may help?)
Linux users can use inotify
inotify is a Linux kernel subsystem
that provides file system event
notification.
Some goodies for Windows fellows:
File Change Notification on MSDN
"When Folders Change" article
File System Notification on Change
The Qt library has a QFileSystemWatcher class which provides cross platform notifications when a file changes. Even if you are not using Qt, because the source is available you could have a look at it as a sample for your own implementation. Qt has separate implementations for Windows, Linux and Mac.
There's File System Events API as of Leopard.
I don't think POSIX itself has facilities for that. The closest to cross-platform I've seen is FAM, which seems to work for Linux, BSD, and Irix, but I'm not how easy it would be to port it to Windows and MacOS.
I've actually built this system before for use in a commercial C++ code base- as long as you don't need every weird thing under the sun, the Windows and POSIX systems have a lot of overlap you can abstract.
POSIX: Use inotify- it is a whole system literally built for this job
Windows: Use "change events". You have to build more of the glue and reporting yourself (all the APIs you need are available, there's just not the 1-stop-shopping inotify gives you).
The common things you can detect in your "notification thread" for forwarding events are:
1) Basically any invasive operation boost::filesystem supports, with the (possible) exception of modifying permissions. This is things like moving, creating, deleting, copying folders and files.
2) Reads and writes to files (esp. writes). Be aware that if you're using async I/O the notifications can show up out-of-order.
3) When a new volume comes in, such as somebody connecting a flash drive.
inotify especially gives you an insane level of fine-grained control, Windows less so. With inotify you can literally monitor everything the filesystem is doing in near-real time if you really want to. I know #3 is possible with both without polling, but be aware that it can be really tricky to get it working correctly- on either system.
I believe OS X now has appropriate hooks/callbacks because they were needed for Spotlight indexing.
On linux you'll have the additional trouble that there are multiple file systems commonly used. If you need the functionality for only a limited amount of files/directories, I'd try about actively looking for modifications at regular intervals.
libevent or libev seem to be what you want, though I haven't used them.