Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Tobster_
Posts: 270
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C
Accepted Solution

C++ or QML Calendar Event Creation Integration

Hello, 

 

I am currently trying to add Calendar event creation integration into my app. 

 

I started out by using the QML Create function here

 

actions: [
        ActionItem {
            title: qsTr("Add To Calendar")
            imageSource: "asset:///IMG/ic_add_entry.png"
            ActionBar.placement: ActionBarPlacement.OnBar
            
            onTriggered: {
                invokeCalendar.trigger("bb.action.CREATE")
            }
        }
    ]
    attachedObjects: [
        Invocation {
            id: invokeCalendar
            query {
                //According to BlackBerry, this doesn't work so I need to find a workaround
                mimeType: "text/calendar"
                invokeActionId: "bb.action.CREATE"
                invokeTargetId: "sys.pim.calendar.viewer.eventcreate"
            }
        }
    ]

 After implementing the code above, I discovered that nothing happened when the actionitem was clicked; so, I queried it with BlackBerry and it turns out they couldn't get it to work either. 

 

So, for the past week I have been trying different methods on how to integrate calendar functionality into my app. I tried the samples here and here but couldn't get either to work due to multiple errors. 

 

Ideally, I'd like to be able to set the: subject, body and start time all from QML but after researching the calendar I don't think I can. 

 

import bb.cascades 1.0

Page {
    property alias fixtureInfo: fixtureText.text
    property alias dateInfo: dateText.text
    property alias timeInfo: timeText.text
    property alias timeZone: timeZoneText.text
    property alias stadiumInfo: stadiumText.text
    titleBar: TitleBar {
        // Localized text with the dynamic translation and locale updates support
        title: qsTr("Fixture") + Retranslate.onLocaleOrLanguageChanged
        scrollBehavior: TitleBarScrollBehavior.Sticky
    }
    ScrollView {
        id: scrollView
        scrollViewProperties {
            scrollMode: ScrollMode.Vertical
            pinchToZoomEnabled: true
        }
        Container {
            id: mainContainer
            Header {
                title: qsTr("Teams Playing")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: fixtureText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            Header {
                title: qsTr("Date")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: dateText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            Header {
                title: qsTr("Time (GMT - 3)")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: timeText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            Header {
                title: qsTr("Time (My Timezone)")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: timeZoneText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            Header {
                title: qsTr("Stadium")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: stadiumText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            attachedObjects: [
                TextStyleDefinition {
                    id: bodyStyle
                    base: SystemDefaults.TextStyles.BodyText
                }
            ]
        }
    }
    actions: [
        ActionItem {
            title: qsTr("Add To Calendar")
            imageSource: "asset:///IMG/ic_add_entry.png"
            ActionBar.placement: ActionBarPlacement.OnBar
            
            onTriggered: {
                invokeCalendar.trigger("bb.action.CREATE")
            }
        },
        ActionItem {
            title: qsTr("Share")
            imageSource: "asset:///IMG/ic_share.png"
            ActionBar.placement: ActionBarPlacement.OnBar
            
            onTriggered: {
                shareAction.trigger("bb.action.SHARE")
            }
        },
        ActionItem {
            title: qsTr("Search For Live Score")
            imageSource: "asset:///IMG/ic_search.png"
            ActionBar.placement: ActionBarPlacement.InOverflow
            
            onTriggered: {
                var searchpage = goToSearch.createObject();
                navigationPane1.push(searchpage);
                searchpage.searchText("live score for " + fixtureText.text)
            }
        }
    ]
    attachedObjects: [
        ComponentDefinition {
            id: goToSearch
            source: "webView.qml"
        },
        Invocation {
            id: invokeCalendar
            query {
                mimeType: "text/calendar"
                invokeActionId: "bb.action.CREATE"
                invokeTargetId: "sys.pim.calendar.viewer.eventcreate"
            }
        },
        Invocation {
            id: shareAction
            query.mimeType: "text/plain"
            query.invokeActionId: "bb.action.SHARE"
            query.data: fixtureText.text + qsTr(" at ") + timeText.text + qsTr(", ") + timeZoneText.text + qsTr(" in my time zone,") + qsTr(" on ") + dateText.text + qsTr(" in the ") + stadiumText.text + " via @TundraCore #WorldCup2014"
            query.onQueryChanged: {
                query.updateQuery()
            }
        }
    ]
}

 

As you can see I am using aliases, in the code above and therefore I'd like to set the following:

 

- Set the title of the event as: "World Cup 2014: " + fixtureText.text
- Set the body as: fixtureText.text + " at " + timeZoneText.text + " on " + dateText.text + " in the " + stadiumText.text
- Set the date as: dateText.text
- Set the start time as: timeZoneText.text

 

Therefore, I was wondering if anyone would be able to show me how to do this in either QML or C++?

Thanks in advance :smileyhappy: 

 

 

 

Developer
Tobster_
Posts: 270
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C

Re: C++ or QML Calendar Event Creation Integration

Update

 


With the help of a couple of developers, I have managed to get a simple calendar integration working. It simply adds a predefined subject and the current date/time as the start and end time to the event. 

 

Now that I managed to get that working, I started to try and use parameters from my QML file but that's where I got confused as I wasn't sure if I was meant to define things fixtureText as a Q String in cpp, hpp and QML or just cpp or etc. 

 

Here's my applicationUI.cpp:

 

#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>

#include "timer.hpp"
#include <bb/cascades/SceneCover>
#include <bb/cascades/Container>

#include <bb/system/SystemDialog>

using namespace bb::cascades;
using namespace bb::system;

ApplicationUI::ApplicationUI() :
        QObject()
{
    // prepare the localization
    m_pTranslator = new QTranslator(this);
    m_pLocaleHandler = new LocaleHandler(this);

    bool res = QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this,
            SLOT(onSystemLanguageChanged()));
    // This is only available in Debug builds
    Q_ASSERT(res);
    // Since the variable is not used in the app, this is added to avoid a
    // compiler warning
    Q_UNUSED(res);

    // initial load
    onSystemLanguageChanged();

    qmlRegisterType<Timer>("CustomTimer", 1, 0, "Timer");

    // Create scene document from main.qml asset, the parent is set
    // to ensure the document gets destroyed properly at shut down.
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    qml->setContextProperty("_app", this);

    // Create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    // Set created root object as the application scene
    Application::instance()->setScene(root);
calendarService = new bb::pim::calendar::CalendarService();

    QmlDocument *qmlCover = QmlDocument::create("asset:///AppCover.qml").parent(this);

    if (!qmlCover->hasErrors()) {
        // Create the QML Container from using the QMLDocument.
        Container *coverContainer = qmlCover->createRootObject<Container>();

        // Create a SceneCover and set the application cover
        SceneCover *sceneCover = SceneCover::create().content(coverContainer);
        Application::instance()->setCover(sceneCover);
    }
}

void ApplicationUI::onSystemLanguageChanged()
{
    QCoreApplication::instance()->removeTranslator(m_pTranslator);
    // Initiate, load and install the application translation files.
    QString locale_string = QLocale().name();
    QString file_name = QString("WorldCup2014_%1").arg(locale_string);
    if (m_pTranslator->load(file_name, "app/native/qm")) {
        QCoreApplication::instance()->installTranslator(m_pTranslator);
    }
}

void ApplicationUI::doCalendar(int intDays)
{
    qDebug() << "entered calendar" << intDays;
    intDays++;
    bb::pim::calendar::CalendarEvent ev;
    ev.setAccountId(1);
    ev.setFolderId(1);

    //QDateTime dateTime = QDateTime::fromString("130", "Mm");
    ev.setStartTime(QDateTime().currentDateTime().addDays(intDays));

    qDebug() << QDateTime().currentDateTime().addDays(intDays);
    ev.setEndTime(QDateTime().currentDateTime().addDays(intDays));
    ev.setSubject(tr("Recharge Watch"));

    // save it to the database
    calendarService->createEvent(ev);

}

 ApplicationUI.hpp:

 

#ifndef ApplicationUI_HPP_
#define ApplicationUI_HPP_

#include <QObject>

#include <bb/pim/calendar/CalendarEvent>
#include <bb/pim/calendar/CalendarService>

namespace bb
{
    namespace cascades
    {
        class LocaleHandler;
    }
}

class QTranslator;

/*!
 * @brief Application UI object
 *
 * Use this object to create and init app UI, to create context objects, to register the new meta types etc.
 */
class ApplicationUI: public QObject
{
    Q_OBJECT
public:
    ApplicationUI();
    virtual ~ApplicationUI()
    {
    }
    Q_INVOKABLE
    void doCalendar(int intDays);
void onSystemLanguageChanged();
private:
    QTranslator* m_pTranslator;
    bb::cascades::LocaleHandler* m_pLocaleHandler;
    bb::pim::calendar::CalendarService* calendarService;
};

#endif /* ApplicationUI_HPP_ */

 FixturesDetail.qml (QML parameters page):

 

import bb.cascades 1.0
import bb.system 1.0

Page {
    property alias fixtureInfo: fixtureText.text
    property alias dateInfo: dateText.text
    property alias timeInfo: timeText.text
    property alias timeZone: timeZoneText.text
    property alias stadiumInfo: stadiumText.text
    titleBar: TitleBar {
        // Localized text with the dynamic translation and locale updates support
        title: qsTr("Fixture") + Retranslate.onLocaleOrLanguageChanged
        scrollBehavior: TitleBarScrollBehavior.Sticky
    }
    ScrollView {
        id: scrollView
        scrollViewProperties {
            scrollMode: ScrollMode.Vertical
            pinchToZoomEnabled: true
        }
        Container {
            id: mainContainer
            Header {
                title: qsTr("Teams Playing")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: fixtureText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            Header {
                title: qsTr("Date")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: dateText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            Header {
                title: qsTr("Time (GMT - 3)")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: timeText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            Header {
                title: qsTr("Time (My Timezone)")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: timeZoneText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            Header {
                title: qsTr("Stadium")
            }
            Container {
                leftPadding: 20
                rightPadding: 20
                Label {
                    id: stadiumText
                    multiline: true
                    textStyle.base: bodyStyle.style
                }
            }
            Divider {
            }
            attachedObjects: [
                TextStyleDefinition {
                    id: bodyStyle
                    base: SystemDefaults.TextStyles.BodyText
                }
            ]
        }
    }
    actions: [
        ActionItem {
            title: qsTr("Add To Calendar")
            imageSource: "asset:///IMG/ic_add_entry.png"
            ActionBar.placement: ActionBarPlacement.OnBar

            onTriggered: {
                _app.doCalendar(-1);
                calendarToast.show();
            }
        },
        ActionItem {
            title: qsTr("Share")
            imageSource: "asset:///IMG/ic_share.png"
            ActionBar.placement: ActionBarPlacement.OnBar

            onTriggered: {
                shareAction.trigger("bb.action.SHARE")
            }
        },
        ActionItem {
            title: qsTr("Search For Live Score")
            imageSource: "asset:///IMG/ic_search.png"
            ActionBar.placement: ActionBarPlacement.InOverflow

            onTriggered: {
                var searchpage = goToSearch.createObject();
                navigationPane1.push(searchpage);
                searchpage.searchText("live score for " + fixtureText.text)
            }
        },
        ActionItem {
            title: qsTr("Update BBM PM")
            imageSource: "asset:///IMG/ic_bbm.png"

            ActionBar.placement: ActionBarPlacement.InOverflow
            
            onTriggered: {
                _app.updatePersonalMessage(qsTr("I'm preparing for ") + fixtureText.text + qsTr(" at ") + timeZoneText.text + qsTr(" on ") + dateText.text)
            }
        }
    ]
    attachedObjects: [
        ComponentDefinition {
            id: goToSearch
            source: "webView.qml"
        },
        Invocation {
            id: invokeCalendar
            query {
                mimeType: "text/calendar"
                invokeActionId: "bb.action.CREATE"
                invokeTargetId: "sys.pim.calendar.viewer.eventcreate"
            }
        },
        SystemToast {
            id: calendarToast
            body: qsTr("Successfully added to calendar")
        },
        Invocation {
            id: shareAction
            query.mimeType: "text/plain"
            query.invokeActionId: "bb.action.SHARE"
            query.data&colon; fixtureText.text + qsTr(" at ") + timeText.text + qsTr(", ") + timeZoneText.text + qsTr(" in my time zone,") + qsTr(" on ") + dateText.text + qsTr(" in the ") + stadiumText.text + " via @TundraCore #WorldCup2014"
            query.onQueryChanged: {
                query.updateQuery()
            }
        }
    ]
}

 So, what I am trying to now do is:

 

- Set the title of the event as: "World Cup 2014: " + fixtureText
- Set the body as: fixtureText+ " at " + timeZoneText + " on " + dateText + " in the " + stadiumText
- Set the date as: dateText
- Set the start time as: timeZoneText

 

Any help with finishing this up would be great. 

 

Thanks :smileyhappy: 

 

 

Developer
Tobster_
Posts: 270
Registered: ‎07-20-2012
My Device: Blackberry Z10 (White), BlackBerry Dev Alpha C

Re: C++ or QML Calendar Event Creation Integration

With the help of two other developers, I managed to get this to work by calling the QML parameters in my hpp file