Hi everyone! In this tutorial I'd like to share an easy way to use CAknGlobalNote right from QML. I know that those things could seem to be rather obvious to experienced developers, but anyway, I decided to share this stuff here.
First, we have to modify our .pro file, to load specific Symbian libraries. Just add this stuff there:
symbian {
LIBS += -lavkon \
-laknnotify \
-leiksrv
}
Then, create a new C++ class. Make it "QObject" type. I named it simply globalnote, but it's not really important, I guess.
In both header and C++ file we have to include aknGlobalNote.h.
#include <aknglobalnote.h>
We have basically empty file, but the includes are ok, so let's strecht your fingers and do some serious coding! Or just copy this stuff, nevermind. First, we need to convert QString to TDesC, which is used by native Symbian APIs. Then, I've just passed it all to another function, which actually controls displaying the global note. Here it is:
void globalnote::displayGlobalNote(QString message)
{
TPtrC16 aMessage(reinterpret_cast<const TUint16*>(message.utf16()));
ShowNoteL(aMessage);
}
The "ShowNoteL" function itself is fairly simple. Interfaces with two another functions, to start displaying note and stop after some period of time. I guess I've just copied this stuff from some example on the Wiki, but nevermind.
void globalnote::ShowNoteL(const TDesC16& aMessage)
{
ShowGlobalNoteL(EAknGlobalInformationNote, aMessage);
User::After(800000);
StopGlobalNoteL();
}
Interesting fact is, that you can replace "EAknGlobalInformationNote" with loads of other options, like:
- EAknGlobalWarningNote
- EAknGlobalConfirmationNote
- EAknGlobalErrorNote
- EAknGlobalChargingNote
- EAknGlobalWaitNote
- EAknGlobalPermanentNote
- EAknGlobalNotChargingNote
- EAknGlobalBatteryFullNote
- EAknGlobalBatteryLowNote
- EAknGlobalRechargeBatteryNote
- EAknCancelGlobalNote
- EAknGlobalTextNote
And here comes another ancient function for those native GlobalNotes. Creates a new CAknGlobalNote and displays it.
void globalnote::ShowGlobalNoteL(TAknGlobalNoteType aNoteType, const TDesC16& aMessage)
{
iNote = CAknGlobalNote::NewL();
iNoteId = iNote->ShowNoteL(aNoteType,aMessage);
}
And finally, closing:
void globalnote::StopGlobalNoteL(void)
{
if(iNote && iNoteId >= 0)
{
iNote->CancelNoteL(iNoteId);
}
iNoteId = -1;
}
So, just copy all this stuff to C++ file. And now, the header file. I guess it doesn't need any explanations, so I'm just simply posting it's contents.
#ifndef GLOBALNOTE_H
#define GLOBALNOTE_H
#include <QObject>
#include <aknglobalnote.h>
class globalnote : public QObject
{
Q_OBJECT
private:
TInt iNoteId;
CAknGlobalNote* iNote;
void StopGlobalNoteL(void);
void ShowGlobalNoteL(TAknGlobalNoteType aNoteType, const TDesC16& aMessage);
void ShowNoteL(const TDesC& aMessage);
public:
explicit globalnote(QObject *parent = 0);
Q_INVOKABLE void displayGlobalNote(QString message);
signals:
public slots:
};
#endif // GLOBALNOTE_H
Congratulations! You now have a C++ class, which you can use in Qt for displaying Global Notes. But why stopping here anyway? Let's make it available to QML. Simple, basic stuff. First, let's include our brand-new, shiny class in main.cpp.
#include "globalnote.h"
And now, in the same file, register it as a QML object.
qmlRegisterType<globalnote>("someVeryAwesomeStuff", 1, 0, "GlobalNote");
Feel free to replace "someVeryAwesomeStuff" with anything you want. You will use this name later in QML file. Those numbers is version. "GlobalNote" is the name of our new QML object.
Let's go to QML file, for example main.qml. First, we need to import some Very Awesome Stuff.
import someVeryAwesomeStuff 1.0
(yup, that's why we needed this name and version number before
)
Then, just add this:
GlobalNote {
id: notify
}
And we can now show a GlobalNote anytime we want.
This is how it looks like in my app:
notify.displayGlobalNote("New message from " + getNameByJid(bareJidLastMsg) + ". You have " + globalUnreadCount + " unread messages.")
Have fun.