root/trunk/apufba/Apufba.d

Revision 21, 7.8 kB (checked in by Carlos, 4 years ago)

Updated for latest Tango revisions

Line 
1 /*
2  Copyright (c) 2007, Carlos Santander Bernal
3
4  This software is provided 'as/is', without any express or implied warranty. In
5  no event will the authors be held liable for any damages arising from the use
6  of this software.
7
8  Permission is granted to anyone to use this software for any purpose, including
9  commercial applications, and to alter it and redistribute it freely, subject to
10  the following restrictions:
11
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software in
14  a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19
20  3. This notice may not be removed or altered from any source distribution.
21  */
22 module apufba.Apufba;
23
24 import apufba.platf.LaunchBrowser;
25
26 import fbd.FacebookClient : FacebookClient, PropertyInfo;
27 import fbd.FacebookError : FacebookError;
28 import fbd.FacebookTypes : Album;
29 import fbd.FacebookUser : FacebookUser;
30
31 import tango.io.File : File;
32 import tango.io.FileConduit : FileConduit;
33 import tango.io.FilePath : FilePath;
34 import tango.net.http.HttpGet : HttpGet;
35 import tango.text.Util : locate, trim;
36 import tango.text.convert.Integer : toString;
37 import tango.text.stream.LineIterator : LineIterator;
38 import tango.util.log.Appender : Appender;
39 import tango.util.log.ConsoleAppender : ConsoleAppender;
40 import tango.util.log.Event : LogEvent = Event;
41 import tango.util.log.EventLayout : EventLayout;
42 import tango.util.log.Log : Log, Logger;
43
44 import wx.wx;
45
46 int main (char[][] args)
47 {
48     auto app = new ApufbaApp ();
49     app.Run ();
50     return 0;
51 }
52
53 private const ApplicationTitle = "Apufba";
54
55 class ApufbaApp : App
56 {
57     override bool OnInit ()
58     {
59         char[] apikey, secret;
60
61         try
62         {
63             /*
64              'account' should be:
65              API Key:       your-api-key
66              Secret:    your-secret
67              */
68             auto account = new FileConduit ("account");
69             auto it = new LineIterator!(char) (account);
70
71             char[] tmp = it.next ();
72             auto pos = locate (tmp, ':');
73             apikey = trim (tmp[pos + 1 .. $]);
74
75             tmp = it.next ();
76             pos = locate (tmp, ':');
77             secret = trim (tmp[pos + 1 .. $]);
78         }
79         catch (Exception e)
80         {
81             MessageBox (e.toString, ApplicationTitle,
82                 Dialog.wxOK | Dialog.wxCENTRE | Dialog.wxICON_ERROR);
83             return false;
84         }
85
86         try
87         {
88             auto fbclient = new FacebookClient (apikey, secret);
89
90             auto frame = new ApufbaFrame (this, fbclient);
91             log = new LogFrame (frame);
92             frame.Show (true);
93             log.Show (true);
94
95             log.logger.info ("API Key: " ~ apikey);
96             log.logger.info ("Secret: " ~ secret);
97         }
98         catch (Object o)
99             MessageBox (o.toString, ApplicationTitle,
100                 Dialog.wxOK | Dialog.wxCENTRE | Dialog.wxICON_ERROR);
101
102         return true;
103     }
104
105     LogFrame log;
106 }
107
108 class ApufbaFrame : Frame
109 {
110     this (ApufbaApp app, FacebookClient client)
111     {
112         super (ApplicationTitle, wxDefaultPosition, Size (600, 600));
113         this.app = app;
114         fbclient = client;
115
116         version (Windows)
117         {
118             BackgroundColour = SystemSettings.GetColour (
119                 SystemColour.wxSYS_COLOUR_BTNFACE);
120         }
121
122         top = new Panel (this, wxDefaultPosition, Size (600, 50));
123         content = new Panel (this, Point (0, 50), Size (600, 550));
124         auto boxSizer = new BoxSizer (Orientation.wxVERTICAL);
125         boxSizer.Add (top, Alignment.wxALIGN_TOP);
126         boxSizer.Add (content, Stretch.wxEXPAND);
127         sizer = boxSizer;
128
129         list = new ListBox (content, Point (8, 8), Size (150, 550));
130         with (list)
131         {
132             Append ("My profile");
133             Append ("Friends");
134             Append ("Notifications");
135             Append ("Photos");
136             SetSelection (0, true);
137             Select_Add (&selectView);
138         }
139
140         main = new Panel (content, wxDefaultPosition, Size (400, 400));
141         boxSizer = new BoxSizer (Orientation.wxHORIZONTAL);
142         boxSizer.Add (list, Alignment.wxALIGN_LEFT);
143         boxSizer.AddSpacer (8);
144         boxSizer.Add (main, Stretch.wxEXPAND);
145         content.sizer = boxSizer;
146         boxSizer.Layout ();
147         content.Hide ();
148
149         auto st = new StaticText (top,
150             "You need to log in to Facebook to start using Apufba",
151             Point (8, 8));
152         auto login = new Button (top, "Log in", Point (st.Right + 8, st.Top));
153         login.Click_Add (&logIn);
154
155         size (BestSize ());
156         CenterOnScreen ();
157     }
158
159     void logIn (Object sender, Event e)
160     {
161         fbclient.authenticate ();
162
163         auto loginurl = fbclient.loginURL ();
164         app.log.logger.info ("Login URL: " ~ loginurl);
165
166         launchBrowser (loginurl);
167         MessageBox (
168             this,
169             "A browser window has been launched.\nAfter logging in to Facebook, click Ok to continue.",
170             ApplicationTitle);
171
172         fbclient.getSession ();
173         auto user = fbclient.getThisUserInfo (FacebookUser.Fields.NAME);
174
175         top.DestroyChildren ();
176         new StaticText (top, "Welcome, " ~ user.name ~ "!", Point (8, 8));
177         content.Show ();
178
179         profileView ();
180
181         size (BestSize ());
182         CenterOnScreen ();
183     }
184
185     void selectView (Object sender, Event e)
186     {
187         main.DestroyChildren ();
188
189         switch ((cast(CommandEvent) e).Selection)
190         {
191             case 0:
192                 // profile
193                 profileView ();
194                 break;
195             case 1:
196                 // friends
197                 friendsView ();
198                 break;
199             case 2:
200                 // notifications
201                 notificationsView ();
202                 break;
203             case 3:
204                 // photos
205                 photosView ();
206                 break;
207             default:
208         }
209     }
210
211     void profileView ()
212     {
213         app.log.logger.info ("getting user profile");
214         auto user = fbclient.getThisUserInfo (FacebookUser.Fields.BIRTHDAY,
215             FacebookUser.Fields.ABOUT_ME, FacebookUser.Fields.PIC_BIG);
216         new StaticText (main, "Birthday: " ~ user.birthday, Point (8, 8));
217         new StaticText (main, "About me: " ~ user.aboutMe, Point (8, 32), Size (
218             300, 160));
219
220         insertImage (user.picBig, main, Point (8, 240));
221     }
222
223     void friendsView ()
224     {
225     }
226
227     void notificationsView ()
228     {
229     }
230
231     void photosView ()
232     {
233         app.log.logger.info ("getting albums");
234         albums = fbclient.getThisUserAlbums ();
235
236         if (albums.length == 0)
237         {
238             new StaticText (main, "You don't have any albums!", Point (8, 8));
239             return;
240         }
241
242         with (new Choice (main, Point (8, 8), wxDefaultSize))
243         {
244             Selected_Add (&changeAlbum);
245             foreach (alb; albums)
246                 Append (alb.name);
247             Selection = 0;
248         }
249     }
250
251     void changeAlbum (Object sender, Event e)
252     {
253         app.log.logger.info ("getting one photo");
254         auto photos = fbclient.getPhotosById (
255             albums[(cast(CommandEvent) e).Selection].coverPhotoId);
256         insertImage (photos[0].bigSrc, main, Point (8, 100));
257     }
258
259     void insertImage (char[] url, Panel where, Point pos)
260     {
261         app.log.logger.info (url);
262         auto f = (new FilePath (url)).file;
263         (new File (f)).write ((new HttpGet (url)).read);
264         app.log.logger.info ("image: " ~ f);
265
266         scope pic = new Bitmap (new Image (f));
267         app.log.logger.info (
268             "dimensions: " ~ .toString (pic.Width) ~ "," ~ .toString (
269                 pic.Height));
270         new StaticBitmap (where, pic, pos, Size (pic.Width, pic.Height));
271
272         (new FilePath (f)).remove ();
273     }
274
275     ApufbaApp app;
276     FacebookClient fbclient;
277     Panel top, content, main;
278     ListBox list;
279     Album[] albums;
280 }
281
282 class LogFrame : Frame
283 {
284     this (Frame parent)
285     {
286         super (parent, "Log", wxDefaultPosition, Size (
287             SystemSettings.GetMetric (SystemMetric.wxSYS_SCREEN_X), 200));
288         auto logList = new ListBox (this);
289         auto f = new Font ();
290         version (darwin)
291         {
292             f.FaceName = "Monaco";
293             f.PointSize = 9;
294         }
295         else version (Windows)
296         {
297             f.FaceName = "Lucida Console";
298             f.PointSize = 9;
299         }
300         logList.SetFont (f);
301         logger = Log.getLogger ("apufba");
302         logger.addAppender (new ListBoxAppender (logList));
303         logger.addAppender (new ConsoleAppender ());
304     }
305
306     Logger logger;
307 }
308
309 // based on ConsoleAppender
310 class ListBoxAppender : Appender
311 {
312     private Mask mask;
313     private ListBox list;
314
315     this (ListBox list, EventLayout layout = null)
316     {
317         mask = register (getName);
318         setLayout (layout);
319         this.list = list;
320     }
321
322     override Mask getMask ()
323     {
324         return mask;
325     }
326
327     override char[] getName ()
328     {
329         return this.classinfo.name;
330     }
331
332     override void append (LogEvent event)
333     {
334         synchronized (list)
335         {
336             auto layout = getLayout;
337             list.Append (
338                 layout.header (event) ~ layout.content (event) ~ layout.footer (
339                     event));
340         }
341     }
342 }
Note: See TracBrowser for help on using the browser.