-
Notifications
You must be signed in to change notification settings - Fork 3
/
classclockywidget.pas
594 lines (450 loc) · 14.1 KB
/
classclockywidget.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
unit ClassClockyWidget;
{$mode objfpc}{$H+}
interface
uses Classes, Graphics, SysUtils, httpsend, superobject, inifiles, clockyutils
,bgrabitmap, bgrabitmaptypes
;
const CLOCKY_CHECK_INTERVAL = 15 * 60;
const CLOCKY_PROFILE_COUNT = 8;
const CLOCKY_PROVIDER_OPENWEATHER = 0;
const CLOCKY_PROVIDER_YAHOO = 1;
{$ifdef Windows}
const CLOCKY_FONT = 'Arial';
{$endif}
{$ifdef Linux}
const CLOCKY_FONT ='sans-serif';
{$endif}
type
TClockyWeatherConditions = record
CurrentTemp: integer;
Description: string;
Icon: string;
Code: integer;
end;
{ TClockyWidget }
TClockyWidget = class( TObject)
ProfileID: integer;
ExePath: string;
TickCount: int64;
LastMove: int64;
LastConditionsCheck: int64;
LocalTimeZone: double; //ie, time zone where computer is
LocationTimeZone: double; //location of where we're checking weather
LocationTitle: string;
Location: string; //whats passed to the api
TimeFormat: string;
DateFormat: string;
TempUnit: string; //F or C
BackgroundColor: integer;
Flat: boolean;
DefaultFontName: string;
FontName: string;
//
Top: integer;
Left: integer;
//
Conditions: TClockyWeatherConditions;
//resources
//Shiny: TPortableNetworkGraphic;
//ShinyBottom: TPortableNetworkGraphic;
Shiny: TBGRABitmap;
ShinyBottom: TBGRABitmap;
//Icons: array[0..100] of TPortableNetworkGraphic;
Icons: array[0..100] of TBGRABitmap;
WeatherProvider: integer;
//API Keys
OpenWeatherMapAPIKey: string;
constructor create( sPath: string);
function GetProfileName(n: integer): string;
function GetTimeStr(): string;
function GetDateStr(): string;
procedure Render( c: TCanvas; width: integer; height: integer);
procedure UpdateWeatherConditions;
procedure UpdateWeatherConditionsOpenWeather;
procedure UpdateWeatherConditionsYahoo;
function iconOpenWeatherToVCloud( sOW: string): integer;
function yahooCodeToVCloud( nCode: integer): integer;
procedure Load;
procedure Save;
function isProfileAutolaunch( nProfile: integer): boolean;
end;
implementation
{ TClockyWidget }
constructor TClockyWidget.create( sPath: string);
var
i: integer;
begin
ExePath := sPath;
ProfileID := 1;
LocationTitle := 'State College';
Location := 'State College,PA';
TimeFormat := 'hh:nnam/pm';
DateFormat := 'ddd, mmm dd, yyyy';
BackgroundColor := $600000;
DefaultFontName := CLOCKY_FONT;
WeatherProvider := CLOCKY_PROVIDER_YAHOO; //OPENWEATHER;
Conditions.Code := 100;
{
Shiny := TPortableNetworkGraphic.Create;
Shiny.LoadFromFile( ExePath + 'graphics/shiny-top.png');
ShinyBottom := TPortableNetworkGraphic.Create;
ShinyBottom.LoadFromFile( ExePath + 'graphics/shiny-bottom.png');
}
Shiny := TBGRABitmap.Create( ExePath + 'graphics/shiny-top.png');
ShinyBottom := TBGRABitmap.Create( ExePath + 'graphics/shiny-bottom.png');
for i := 0 to 100 do
begin
icons[i] := nil;
end;
end;
function TClockyWidget.GetProfileName(n: integer): string;
var
ini: TIniFile;
sFile: string;
begin
sFile := GetAppConfigFile( false, true);
ini := TIniFile.Create( sFile);
result := ini.ReadString( 'Prof_' + IntToStr( n), 'LocationTitle', '(Unassigned)');
ini.Free;
end;
function TClockyWidget.GetTimeStr: string;
begin
result := FormatDateTime( TimeFormat, clockyTimeAtZone( LocalTimeZone, LocationTimeZone));
end;
function TClockyWidget.GetDateStr: string;
begin
result := FormatDateTime( DateFormat, clockyTimeAtZone( LocalTimeZone, LocationTimeZone));
end;
procedure TClockyWidget.Render(c: TCanvas; width: integer; height: integer);
var
tw: integer;
x: integer;
y: integer;
s: string;
png: TPortableNetworkGraphic;
nIcon: integer;
sIcon: string;
tmppng: TBGRABitmap;
begin
//background
c.Brush.Style := bsSolid;
c.Brush.Color := BackgroundColor; //RGBToColor( 0, 0, 24);
c.FillRect( 0, 0, width, height);
//c.Rectangle( 0, 0, width, height);
//draw glassy edge
if not Flat then
begin
//c.Draw(0, 0, Shiny);
//c.Draw(0, height - ShinyBottom.Height, ShinyBottom);
Shiny.Draw( c, 0, 0, False);
ShinyBottom.Draw( c, 0, height - ShinyBottom.Height, false);
end;
//Location Header
c.Font.Name := FontName; // 'Utah';
c.Font.Color := clWhite;
c.Font.Height := 20;
c.Brush.Style := bsClear;
tw := c.GetTextWidth( LocationTitle);
x := round( (width/2) - (tw/2));
c.TextOut( x, 10, LocationTitle);
//Time
c.Font.Height := 40;
s := GetTimeStr;
tw := c.GetTextWidth( s);
x := round( (width/2) - (tw/2));
c.TextOut( x, 30, s);
//date
c.Font.Height := 20;
s := GetDateStr;
tw := c.GetTextWidth( s);
x := round( (width/2) - (tw/2));
c.TextOut( x, 70, s);
//temp
c.Font.Height := 40;
s := IntToStr( Conditions.CurrentTemp) + '°';
tw := c.GetTextWidth( s);
//x := round( (width/4) - (tw/2) + (width/2));
x := round((width /2) + 5);
c.TextOut( x, 110, s);
//icon
case WeatherProvider of
CLOCKY_PROVIDER_YAHOO:
begin
nIcon := yahooCodeToVCloud( Conditions.Code);
end;
else
nIcon := iconOpenWeatherToVCloud( Conditions.Icon);
end;
if Icons[nIcon] = nil then
begin
//Icons[nIcon] := TPortableNetworkGraphic.Create;
//Icons[nIcon].LoadFromFile( ExePath + 'icons/VClouds/' +IntToStr( nIcon) + '.png');
tmppng := TBGRABitmap.Create( ExePath + 'icons/VClouds/' +IntToStr( nIcon) + '.png');
tmppng.ResampleFilter:= rfBicubic;
Icons[nIcon] := tmppng.Resample(82, 60) as TBGRABitmap;
tmppng.Free;
//Icons[nIcon] := TBGRABitmap.Create( ExePath + 'icons/VClouds/' +IntToStr( nIcon) + '.png');
end;
if Icons[nIcon] <> nil then
begin
//c.Draw( 0, 100, Icons[nIcon]);
//x := round( (width / 4) - 30);
x := round((width / 2) - 82 - 5);
//c.StretchDraw( rect( x, 100, x+82, 160), Icons[nIcon]);
Icons[nIcon].draw( c, x, 100, false);
end;
//conditions
c.Font.Height := 20;
s := Conditions.Description;
tw := c.GetTextWidth( s);
x := round( (width/2) - (tw/2));
c.TextOut( x, 160, s);
end;
procedure TClockyWidget.UpdateWeatherConditions;
begin
case WeatherProvider of
CLOCKY_PROVIDER_YAHOO:
UpdateWeatherConditionsYahoo;
else
UpdateWeatherConditionsOpenWeather;
end;
end;
procedure TClockyWidget.UpdateWeatherConditionsOpenWeather;
var
sURL: string;
sj: string;
rs: TStringList;
j: ISuperObject;
sa: TSuperArray;
p: string;
n: integer;
begin
sURL := 'http://api.openweathermap.org/data/2.5/weather?q=' + Location + '&units=imperial&APPID=' + OpenWeatherMapAPIKey;
rs := TStringList.Create;
try
if HTTPGetText( sURL, rs) then
begin
sj := rs.Text;
p := copy( sj, 1, 1);
if (length( sj) > 0) and (p = '{') then
begin
j := so( sj);
if assigned( j) then
begin
try
n := j['main'].i['temp'];
if (TempUnit = 'C') then
begin
Conditions.CurrentTemp:= clockyFtoC( n);
end
else
begin
Conditions.CurrentTemp:= n;
end;
sa := j.A['weather'];
if sa.Length > 0 then
begin
Conditions.Description:= sa[0].s['main'];
Conditions.Icon:= sa[0].s['icon'];
end;
except
end;
end;
end;
end;
finally
rs.Free;
end;
end;
procedure TClockyWidget.UpdateWeatherConditionsYahoo;
var
sURL: string;
sj: string;
rs: TStringList;
j: ISuperObject;
sa: TSuperArray;
p: string;
n: integer;
yql: string;
begin
sURL := 'http://query.yahooapis.com/v1/public/yql?q=';
yql := 'select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + Location + '")';
yql := StringReplace( yql, ' ', '%20', [rfReplaceAll]);
yql := StringReplace( yql, '=', '%3D', [rfReplaceAll]);
yql := StringReplace( yql, '"', '%22', [rfReplaceAll]);
yql := StringReplace( yql, ',', '%2C', [rfReplaceAll]);
sURL := sURL + yql + '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
rs := TStringList.Create;
try
if HTTPGetText( sURL, rs) then
begin
sj := rs.Text;
p := copy( sj, 1, 1);
if (length( sj) > 0) and (p = '{') then
begin
j := so( sj);
if assigned( j) then
begin
try
p := j['query'].o['results'].o['channel'].o['item'].o['condition'].S['temp'];
if TryStrToInt( p, n) then
begin
if uppercase( TempUnit) = 'C' then
begin
Conditions.CurrentTemp := clockyFtoC(n);
end
else
begin
Conditions.CurrentTemp := n;
end;
end;
p := j['query'].o['results'].o['channel'].o['item'].o['condition'].S['code'];
if TryStrToInt( p, n) then
begin
Conditions.Code := n;
end;
Conditions.Description := j['query'].o['results'].o['channel'].o['item'].o['condition'].S['text'];
except
end;
end;
end;
end;
finally
rs.Free;
end;
end;
function TClockyWidget.iconOpenWeatherToVCloud(sOW: string): integer;
begin
if sOW = '01d' then
result := 32
else if sOW = '01n' then
result := 31
else if sOW = '02d' then
result := 30
else if sOW = '02n' then
result := 33
else if sOW = '03d' then
result := 28
else if sOW = '03n' then
result := 27
else if sOW = '04d' then
result := 26
else if sOW = '04n' then
result := 26
else if sOW = '09d' then
result := 9
else if sOW = '09n' then
result := 9
else if sOW = '10d' then
result := 0
else if sOW = '10n' then
result := 0
else if sOW = '11d' then
result := 4
else if sOW = '11n' then
result := 4
else if sOW = '13d' then
result := 14
else if sOW = '13n' then
result := 14
else if sOW = '50d' then
result := 20
else if sOW = '50n' then
result := 20
else
result := 100;
end;
function TClockyWidget.yahooCodeToVCloud(nCode: integer): integer;
begin
//vcloud image set is already mapped to yahoo codes
if (nCode >= 0) and (nCode <= 47) then
begin
result := nCode
end
else
begin
result := 100;
end;
end;
procedure TClockyWidget.Load;
var
sPath, sFile, sSect, sColor: string;
ini: TIniFile;
p: string;
begin
sFile := GetAppConfigFile( false, true);
sPath := ExtractFilePath( sFile);
ForceDirectories( sPath);
ini := TIniFile.Create( sFile);
//global settings
WeatherProvider := CLOCKY_PROVIDER_YAHOO; //OPENWEATHER;
p := ini.ReadString( 'clocky', 'WeatherProvider', '');
if (lowercase(p) = 'yahoo') then
begin
WeatherProvider := CLOCKY_PROVIDER_YAHOO;
end;
LocalTimeZone := ini.ReadFloat( 'clocky', 'LocalTimeZone', 0);
OpenWeatherMapAPIKey := ini.ReadString( 'clocky', 'OpenWeatherMapAPIKey', '');
DefaultFontName := ini.ReadString( 'clocky', 'DefaultFontName', CLOCKY_FONT);
//profile specific settings
sSect := 'Prof_' + IntToStr( ProfileID);
p := ini.ReadString( sSect, 'WeatherProvider', ''); //to override global
if (lowercase(p) = 'yahoo') then
begin
WeatherProvider := CLOCKY_PROVIDER_YAHOO;
end;
if (lowercase(p) = 'openweather') then
begin
WeatherProvider := CLOCKY_PROVIDER_OPENWEATHER;
end;
LocationTitle := ini.ReadString( sSect, 'LocationTitle', 'State College');
Location := ini.ReadString( sSect, 'Location', 'State College, PA');
LocationTimeZone := ini.ReadFloat( sSect, 'LocationTimeZone', LocalTimeZone);
TempUnit := ini.ReadString( sSect, 'Unit', 'F');
Left := ini.ReadInteger( sSect, 'Left', 200);
Top := ini.ReadInteger( sSect, 'Top', 200);
sColor := ini.ReadString( sSect, 'BackgroundColor', '$00000000');
try
BackgroundColor := StringToColor( sColor);
except
end;
Flat := ini.ReadBool( sSect, 'Flat', false);
FontName := ini.ReadString( sSect, 'FontName', DefaultFontName);
ini.Free;
end;
procedure TClockyWidget.Save;
var
sPath, sFile, sSect, sColor: string;
ini: TIniFile;
begin
sFile := GetAppConfigFile( false, true);
sPath := ExtractFilePath( sFile);
ForceDirectories( sPath);
ini := TIniFile.Create( sFile);
sSect := 'Prof_' + IntToStr( ProfileID);
ini.WriteString( sSect, 'LocationTitle', LocationTitle);
ini.WriteString( sSect, 'Location', Location);
ini.WriteInteger( sSect, 'Left', Left);
ini.WriteInteger( sSect, 'Top', Top);
sColor := ColorToString( BackgroundColor);
ini.WriteString( sSect, 'BackgroundColor', sColor);
ini.WriteBool( sSect, 'Flat', Flat);
ini.WriteString( sSect, 'FontName', FontName);
ini.Free;
end;
function TClockyWidget.isProfileAutolaunch(nProfile: integer): boolean;
var
sFile: string;
sSect: string;
ini: TIniFile;
begin
result := false;
sFile := GetAppConfigFile( false, true);
if not FileExists( sFile) then
exit;
sSect := 'Prof_' + IntToStr( nProfile);
ini := TIniFile.Create( sFile);
result := ini.ReadBool( sSect, 'AutoLaunch', false);
ini.Free;
end;
end.