Update User Agent String
[framework/web/wrt-commons.git] / examples / timed_event / timed_event.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        timed_event.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of timed event example
21  */
22 #include <dpl/generic_event.h>
23 #include <dpl/application.h>
24 #include <dpl/controller.h>
25 #include <dpl/type_list.h>
26 #include <dpl/thread.h>
27 #include <dpl/log/log.h>
28 #include <string>
29
30 DECLARE_GENERIC_EVENT_0(FirstEvent)
31 DECLARE_GENERIC_EVENT_0(SecondEvent)
32
33 class ControllerInThread
34     : public DPL::Controller<DPL::TypeListDecl<FirstEvent,
35                                                SecondEvent>::Type>
36 {
37 protected:
38     virtual void OnEventReceived(const FirstEvent &event)
39     {
40         (void)event;
41         LogInfo("First event occurred");
42     }
43
44     virtual void OnEventReceived(const SecondEvent &event)
45     {
46         (void)event;
47         LogInfo("Second event occurred");
48     }
49 };
50
51 DECLARE_GENERIC_EVENT_0(QuitEvent)
52
53 class MyApplication
54     : public DPL::Application,
55       private DPL::Controller<DPL::TypeListDecl<QuitEvent>::Type>
56 {
57 private:
58     DPL::Thread m_thread;
59     ControllerInThread m_controllerInThread;
60
61     // Quit application event occurred
62     virtual void OnEventReceived(const QuitEvent &event)
63     {
64         (void)event;
65         Quit();
66     }
67
68 public:
69     MyApplication(int argc, char **argv)
70         : Application(argc, argv, "timed_event", false)
71     {
72         // Touch
73         Touch();
74         m_controllerInThread.Touch();
75
76         // Run thread
77         m_thread.Run();
78         m_controllerInThread.SwitchToThread(&m_thread);
79
80         // Emit thread timed events
81         m_controllerInThread.DPL::ControllerEventHandler<SecondEvent>::PostTimedEvent(SecondEvent(), 3);
82         m_controllerInThread.DPL::ControllerEventHandler<FirstEvent>::PostTimedEvent(FirstEvent(), 2);
83
84         // Emit framework timed quit event
85         DPL::ControllerEventHandler<QuitEvent>::PostTimedEvent(QuitEvent(), 5);
86     }
87
88     virtual ~MyApplication()
89     {
90         m_controllerInThread.SwitchToThread(NULL);
91
92         // Quit thread
93         m_thread.Quit();
94     }
95 };
96
97 int main(int argc, char *argv[])
98 {
99     MyApplication app(argc, argv);
100     return app.Exec();
101 }