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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
|
/* gweather-weather.c - Overall weather server functions
*
* SPDX-FileCopyrightText: The GWeather authors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "gweather-weather.h"
#include "gweather-enum-types.h"
#include "gweather-private.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <langinfo.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <gio/gio.h>
#define MOON_PHASES 36
/**
* GWeatherInfo:
*
* `GWeatherInfo` provides a handy way to access weather conditions
* and forecasts from a [struct@GWeather.Location], aggregating multiple
* different web services.
*
* It includes also astronomical data such as sunrise times and
* moon phases.
*/
#define TEMPERATURE_UNIT "temperature-unit"
#define DISTANCE_UNIT "distance-unit"
#define SPEED_UNIT "speed-unit"
#define PRESSURE_UNIT "pressure-unit"
#define DEFAULT_LOCATION "default-location"
enum
{
PROP_0,
PROP_LOCATION,
PROP_ENABLED_PROVIDERS,
PROP_APPLICATION_ID,
PROP_CONTACT_INFO,
PROP_LAST
};
enum
{
SIGNAL_UPDATED,
SIGNAL_LAST
};
static guint gweather_info_signals[SIGNAL_LAST];
G_DEFINE_TYPE (GWeatherInfo, gweather_info, G_TYPE_OBJECT);
void
_gweather_gettext_init (void)
{
static gsize gweather_gettext_initialized = FALSE;
if (G_UNLIKELY (g_once_init_enter (&gweather_gettext_initialized))) {
bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif
bindtextdomain (LOCATIONS_GETTEXT_PACKAGE, GNOMELOCALEDIR);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset (LOCATIONS_GETTEXT_PACKAGE, "UTF-8");
#endif
g_once_init_leave (&gweather_gettext_initialized, TRUE);
}
}
static void
_weather_location_free (WeatherLocation *location)
{
g_free (location->name);
g_free (location->code);
g_free (location->zone);
g_free (location->radar);
g_free (location->country_code);
g_free (location->tz_hint);
}
static gboolean
should_use_caps (GWeatherFormatOptions options)
{
return options == GWEATHER_FORMAT_OPTION_DEFAULT ||
(options & GWEATHER_FORMAT_OPTION_SENTENCE_CAPITALIZATION);
}
/* clang-format off */
static const gchar *wind_direction_str[] = {
N_("variable"),
N_("north"), N_("north — northeast"), N_("northeast"), N_("east — northeast"),
N_("east"), N_("east — southeast"), N_("southeast"), N_("south — southeast"),
N_("south"), N_("south — southwest"), N_("southwest"), N_("west — southwest"),
N_("west"), N_("west — northwest"), N_("northwest"), N_("north — northwest")
};
static const gchar *wind_direction_caps_str[] = {
N_("Variable"),
N_("North"), N_("North — Northeast"), N_("Northeast"), N_("East — Northeast"),
N_("East"), N_("East — Southeast"), N_("Southeast"), N_("South — Southeast"),
N_("South"), N_("South — Southwest"), N_("Southwest"), N_("West — Southwest"),
N_("West"), N_("West — Northwest"), N_("Northwest"), N_("North — Northwest")
};
/* clang-format on */
const gchar *
gweather_wind_direction_to_string_full (GWeatherWindDirection wind,
GWeatherFormatOptions options)
{
gboolean use_caps = should_use_caps (options);
if (wind <= GWEATHER_WIND_INVALID || wind >= GWEATHER_WIND_LAST)
return use_caps ? C_ ("wind direction", "Invalid")
: C_ ("wind direction", "invalid");
return use_caps ? _ (wind_direction_caps_str[(int) wind])
: _ (wind_direction_str[(int) wind]);
}
const gchar *
gweather_wind_direction_to_string (GWeatherWindDirection wind)
{
return gweather_wind_direction_to_string_full (wind, GWEATHER_FORMAT_OPTION_DEFAULT);
}
static const gchar *sky_str[] = {
N_ ("clear sky"),
N_ ("broken clouds"),
N_ ("scattered clouds"),
N_ ("few clouds"),
N_ ("overcast")
};
static const gchar *sky_caps_str[] = {
N_ ("Clear sky"),
N_ ("Broken clouds"),
N_ ("Scattered clouds"),
N_ ("Few clouds"),
N_ ("Overcast")
};
const char *
gweather_sky_to_string (GWeatherSky sky)
{
return gweather_sky_to_string_full (sky, GWEATHER_FORMAT_OPTION_DEFAULT);
}
const gchar *
gweather_sky_to_string_full (GWeatherSky sky,
GWeatherFormatOptions options)
{
gboolean use_caps = should_use_caps (options);
if (sky <= GWEATHER_SKY_INVALID || sky >= GWEATHER_SKY_LAST)
return use_caps ? C_ ("sky conditions", "Invalid")
: C_ ("sky conditions", "invalid");
return use_caps ? _ (sky_caps_str[(int) sky])
: _ (sky_str[(int) sky]);
}
/*
* Even though tedious, I switched to a 2D array for weather condition
* strings, in order to facilitate internationalization, esp. for languages
* with genders.
*/
/*
* Almost all reportable combinations listed in
* http://www.crh.noaa.gov/arx/wx.tbl.php are entered below, except those
* having 2 qualifiers mixed together [such as "Blowing snow in vicinity"
* (VCBLSN), "Thunderstorm in vicinity" (VCTS), etc].
* Combinations that are not possible are filled in with "??".
* Some other exceptions not handled yet, such as "SN BLSN" which has
* special meaning.
*/
/* clang-format off */
/*
* Note, magic numbers, when you change the size here, make sure to change
* the below function so that new values are recognized
*/
/* NONE VICINITY LIGHT MODERATE HEAVY SHALLOW PATCHES PARTIAL THUNDERSTORM BLOWING SHOWERS DRIFTING FREEZING */
/* *******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/
static const gchar *conditions_str[24][13] = {
/* TRANSLATOR: If you want to know what "blowing" "shallow" "partial"
* etc means, you can go to http://www.weather.com/glossary/ and
* http://www.crh.noaa.gov/arx/wx.tbl.php */
/* NONE */ {"??", "??", "??", "??", "??", "??", "??", "??", N_("thunderstorm"), "??", "??", "??", "??" },
/* DRIZZLE */ {N_("drizzle"), "??", N_("light drizzle"), N_("moderate drizzle"), N_("heavy drizzle"), "??", "??", "??", "??", "??", "??", "??", N_("freezing drizzle") },
/* RAIN */ {N_("rain"), "??", N_("light rain"), N_("moderate rain"), N_("heavy rain"), "??", "??", "??", N_("thunderstorm"), "??", N_("rain showers"), "??", N_("freezing rain") },
/* SNOW */ {N_("snow"), "??", N_("light snow"), N_("moderate snow"), N_("heavy snow"), "??", "??", "??", N_("snowstorm"), N_("blowing snowfall"), N_("snow showers"), N_("drifting snow"), "??" },
/* SNOW_GRAINS */ {N_("snow grains"), "??", N_("light snow grains"), N_("moderate snow grains"), N_("heavy snow grains"), "??", "??", "??", "??", "??", "??", "??", "??" },
/* ICE_CRYSTALS */ {N_("ice crystals"), "??", "??", N_("ice crystals"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* ICE_PELLETS */ {N_("sleet"), "??", N_("little sleet"), N_("moderate sleet"), N_("heavy sleet"), "??", "??", "??", N_("sleet storm"), "??", N_("showers of sleet"), "??", "??" },
/* HAIL */ {N_("hail"), "??", "??", N_("hail"), "??", "??", "??", "??", N_("hailstorm"), "??", N_("hail showers"), "??", "??", },
/* SMALL_HAIL */ {N_("small hail"), "??", "??", N_("small hail"), "??", "??", "??", "??", N_("small hailstorm"), "??", N_("showers of small hail"), "??", "??" },
/* PRECIPITATION */ {N_("unknown precipitation"), "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* MIST */ {N_("mist"), "??", "??", N_("mist"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* FOG */ {N_("fog"), N_("fog in the vicinity") , "??", N_("fog"), "??", N_("shallow fog"), N_("patches of fog"), N_("partial fog"), "??", "??", "??", "??", N_("freezing fog") },
/* SMOKE */ {N_("smoke"), "??", "??", N_("smoke"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* VOLCANIC_ASH */ {N_("volcanic ash"), "??", "??", N_("volcanic ash"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* SAND */ {N_("sand"), "??", "??", N_("sand"), "??", "??", "??", "??", "??", N_("blowing sand"), "", N_("drifting sand"), "??" },
/* HAZE */ {N_("haze"), "??", "??", N_("haze"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* SPRAY */ {"??", "??", "??", "??", "??", "??", "??", "??", "??", N_("blowing sprays"), "??", "??", "??" },
/* DUST */ {N_("dust"), "??", "??", N_("dust"), "??", "??", "??", "??", "??", N_("blowing dust"), "??", N_("drifting dust"), "??" },
/* SQUALL */ {N_("squall"), "??", "??", N_("squall"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* SANDSTORM */ {N_("sandstorm"), N_("sandstorm in the vicinity") , "??", N_("sandstorm"), N_("heavy sandstorm"), "??", "??", "??", "??", "??", "??", "??", "??" },
/* DUSTSTORM */ {N_("duststorm"), N_("duststorm in the vicinity") , "??", N_("duststorm"), N_("heavy duststorm"), "??", "??", "??", "??", "??", "??", "??", "??" },
/* FUNNEL_CLOUD */ {N_("funnel cloud"), "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* TORNADO */ {N_("tornado"), "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* DUST_WHIRLS */ {N_("dust whirls"), N_("dust whirls in the vicinity") , "??", N_("dust whirls"), "??", "??", "??", "??", "??", "??", "??", "??", "??" }
};
/*
* Note, magic numbers, when you change the size here, make sure to change
* the below function so that new values are recognized
*/
/* NONE VICINITY LIGHT MODERATE HEAVY SHALLOW PATCHES PARTIAL THUNDERSTORM BLOWING SHOWERS DRIFTING FREEZING */
/* *******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/
static const gchar *conditions_caps_str[24][13] = {
/* TRANSLATOR: If you want to know what "blowing" "shallow" "partial"
* etc means, you can go to http://www.weather.com/glossary/ and
* http://www.crh.noaa.gov/arx/wx.tbl.php */
/* NONE */ {"??", "??", "??", "??", "??", "??", "??", "??", N_("Thunderstorm"), "??", "??", "??", "??" },
/* DRIZZLE */ {N_("Drizzle"), "??", N_("Light drizzle"), N_("Moderate drizzle"), N_("Heavy drizzle"), "??", "??", "??", "??", "??", "??", "??", N_("Freezing drizzle") },
/* RAIN */ {N_("Rain"), "??", N_("Light rain"), N_("Moderate rain"), N_("Heavy rain"), "??", "??", "??", N_("Thunderstorm"), "??", N_("Rain showers"), "??", N_("Freezing rain") },
/* SNOW */ {N_("Snow"), "??", N_("Light snow"), N_("Moderate snow"), N_("Heavy snow"), "??", "??", "??", N_("Snowstorm"), N_("Blowing snowfall"), N_("Snow showers"), N_("Drifting snow"), "??" },
/* SNOW_GRAINS */ {N_("Snow grains"), "??", N_("Light snow grains"), N_("Moderate snow grains"), N_("Heavy snow grains"), "??", "??", "??", "??", "??", "??", "??", "??" },
/* ICE_CRYSTALS */ {N_("Ice crystals"), "??", "??", N_("Ice crystals"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* ICE_PELLETS */ {N_("Sleet"), "??", N_("Little sleet"), N_("Moderate sleet"), N_("Heavy sleet"), "??", "??", "??", N_("Sleet storm"), "??", N_("Showers of sleet"), "??", "??" },
/* HAIL */ {N_("Hail"), "??", "??", N_("Hail"), "??", "??", "??", "??", N_("Hailstorm"), "??", N_("Hail showers"), "??", "??", },
/* SMALL_HAIL */ {N_("Small hail"), "??", "??", N_("Small hail"), "??", "??", "??", "??", N_("Small hailstorm"), "??", N_("Showers of small hail"), "??", "??" },
/* PRECIPITATION */ {N_("Unknown precipitation"), "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* MIST */ {N_("Mist"), "??", "??", N_("Mist"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* FOG */ {N_("Fog"), N_("Fog in the vicinity") , "??", N_("Fog"), "??", N_("Shallow fog"), N_("Patches of fog"), N_("Partial fog"), "??", "??", "??", "??", N_("Freezing fog") },
/* SMOKE */ {N_("Smoke"), "??", "??", N_("Smoke"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* VOLCANIC_ASH */ {N_("Volcanic ash"), "??", "??", N_("Volcanic ash"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* SAND */ {N_("Sand"), "??", "??", N_("Sand"), "??", "??", "??", "??", "??", N_("Blowing sand"), "", N_("Drifting sand"), "??" },
/* HAZE */ {N_("Haze"), "??", "??", N_("Haze"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* SPRAY */ {"??", "??", "??", "??", "??", "??", "??", "??", "??", N_("Blowing sprays"), "??", "??", "??" },
/* DUST */ {N_("Dust"), "??", "??", N_("Dust"), "??", "??", "??", "??", "??", N_("Blowing dust"), "??", N_("Drifting dust"), "??" },
/* SQUALL */ {N_("Squall"), "??", "??", N_("Squall"), "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* SANDSTORM */ {N_("Sandstorm"), N_("Sandstorm in the vicinity") , "??", N_("Sandstorm"), N_("Heavy sandstorm"), "??", "??", "??", "??", "??", "??", "??", "??" },
/* DUSTSTORM */ {N_("Duststorm"), N_("Duststorm in the vicinity") , "??", N_("Duststorm"), N_("Heavy duststorm"), "??", "??", "??", "??", "??", "??", "??", "??" },
/* FUNNEL_CLOUD */ {N_("Funnel cloud"), "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* TORNADO */ {N_("Tornado"), "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??", "??" },
/* DUST_WHIRLS */ {N_("Dust whirls"), N_("Dust whirls in the vicinity") , "??", N_("Dust whirls"), "??", "??", "??", "??", "??", "??", "??", "??", "??" }
};
/* clang-format on */
const gchar *
gweather_conditions_to_string_full (GWeatherConditions *cond,
GWeatherFormatOptions options)
{
gboolean use_caps = should_use_caps (options);
const gchar *str;
if (!cond->significant) {
return "-";
} else {
if (cond->phenomenon > GWEATHER_PHENOMENON_INVALID &&
cond->phenomenon < GWEATHER_PHENOMENON_LAST &&
cond->qualifier > GWEATHER_QUALIFIER_INVALID &&
cond->qualifier < GWEATHER_QUALIFIER_LAST)
str = use_caps ? _ (conditions_caps_str[(int) cond->phenomenon][(int) cond->qualifier])
: _ (conditions_str[(int) cond->phenomenon][(int) cond->qualifier]);
else
str = use_caps ? C_ ("sky conditions", "Invalid")
: C_ ("sky conditions", "invalid");
return (strlen (str) > 0) ? str : "-";
}
}
const gchar *
gweather_conditions_to_string (GWeatherConditions *cond)
{
return gweather_conditions_to_string_full (cond, GWEATHER_FORMAT_OPTION_DEFAULT);
}
static gboolean
requests_init (GWeatherInfo *info)
{
if (info->requests_pending)
return FALSE;
return TRUE;
}
void
_gweather_info_begin_request (GWeatherInfo *info,
SoupMessage *message)
{
info->requests_pending = g_slist_prepend (info->requests_pending, message);
g_object_ref (message);
}
static void
copy_weather_data (GWeatherInfo *src,
GWeatherInfo *dest)
{
dest->hasHumidity = src->hasHumidity;
dest->update = src->update;
dest->current_time = src->current_time;
dest->sky = src->sky;
dest->cond = src->cond;
dest->temp = src->temp;
dest->temp_min = src->temp_min;
dest->temp_max = src->temp_max;
dest->dew = src->dew;
dest->humidity = src->humidity;
dest->wind = src->wind;
dest->windspeed = src->windspeed;
dest->pressure = src->pressure;
dest->visibility = src->visibility;
}
static void
fixup_current_conditions (GWeatherInfo *info)
{
GWeatherInfo *first_forecast;
/* Current conditions already available */
if (info->update != 0) {
g_debug ("Not fixing up current conditions, already valid");
return;
} else if (!info->forecast_list ||
!info->forecast_list->data) {
g_debug ("No forecast list available, not fixing up");
return;
}
first_forecast = info->forecast_list->data;
/* Add current conditions from forecast if close enough */
if (first_forecast->update - time (NULL) > 60 * 60) {
g_debug ("Forecast is too far in the future, ignoring");
return;
}
copy_weather_data (first_forecast, info);
g_debug ("Fixed up missing current weather with first forecast data");
}
void
_gweather_info_request_done (GWeatherInfo *info,
SoupMessage *message)
{
info->requests_pending = g_slist_remove (info->requests_pending, message);
g_object_unref (message);
if (info->requests_pending == NULL) {
fixup_current_conditions (info);
g_signal_emit (info, gweather_info_signals[SIGNAL_UPDATED], 0);
} else {
g_debug ("Not emitting 'updated' as there are still %d requests pending",
g_slist_length (info->requests_pending));
}
}
#if SOUP_CHECK_VERSION (2, 99, 2)
void
_gweather_info_queue_request (GWeatherInfo *info,
SoupMessage *msg,
GAsyncReadyCallback callback)
{
GCancellable *cancellable = g_cancellable_new ();
g_object_set_data_full (G_OBJECT (msg), "request-cancellable",
cancellable, g_object_unref);
soup_session_send_and_read_async (info->session, msg, G_PRIORITY_DEFAULT,
cancellable, callback, info);
g_object_unref (msg);
}
#else
void
_gweather_info_queue_request (GWeatherInfo *info,
SoupMessage *msg,
SoupSessionCallback callback)
{
soup_session_queue_message (info->session, msg, callback, info);
}
#endif
/* it's OK to pass in NULL */
void
free_forecast_list (GWeatherInfo *info)
{
if (!info)
return;
g_slist_free_full (info->forecast_list, g_object_unref);
info->forecast_list = NULL;
}
/* Relative humidity computation - thanks to <Olof.Oberg@modopaper.modogroup.com>
calc_dew is simply the inverse of calc_humidity */
static inline gdouble
calc_dew (gdouble temp, gdouble humidity)
{
gdouble esat, esurf, tmp;
if (temp > -500.0 && humidity > -1.0) {
temp = TEMP_F_TO_C (temp);
esat = 6.11 * pow (10.0, (7.5 * temp) / (237.7 + temp));
esurf = (humidity / 100) * esat;
} else {
esurf = -1.0;
esat = 1.0;
}
tmp = log10 (esurf / 6.11);
return TEMP_C_TO_F (tmp * 237.7 / (tmp + 7.5));
}
static inline gdouble
calc_humidity (gdouble temp, gdouble dewp)
{
gdouble esat, esurf;
if (temp > -500.0 && dewp > -500.0) {
temp = TEMP_F_TO_C (temp);
dewp = TEMP_F_TO_C (dewp);
esat = 6.11 * pow (10.0, (7.5 * temp) / (237.7 + temp));
esurf = 6.11 * pow (10.0, (7.5 * dewp) / (237.7 + dewp));
} else {
esurf = -1.0;
esat = 1.0;
}
return ((esurf / esat) * 100.0);
}
static inline gdouble
calc_apparent (GWeatherInfo *info)
{
gdouble temp = info->temp;
gdouble wind = WINDSPEED_KNOTS_TO_MPH (info->windspeed);
gdouble apparent = -1000.;
gdouble dew = info->dew;
gdouble humidity;
if (info->hasHumidity)
humidity = info->humidity;
else
humidity = calc_humidity (temp, dew);
/*
* Wind chill calculations as of 01-Nov-2001
* http://www.nws.noaa.gov/om/windchill/index.shtml
* Some pages suggest that the formula will soon be adjusted
* to account for solar radiation (bright sun vs cloudy sky)
*/
if (temp <= 50.0) {
if (wind > 3.0) {
gdouble v = pow (wind, 0.16);
apparent = 35.74 + 0.6215 * temp - 35.75 * v + 0.4275 * temp * v;
} else if (wind >= 0.) {
apparent = temp;
}
}
/*
* Heat index calculations:
* http://www.srh.noaa.gov/fwd/heatindex/heat5.html
*/
else if (temp >= 80.0) {
if (temp >= -500. && humidity >= 0) {
gdouble t2, h2;
gdouble t3, h3;
t2 = temp * temp;
h2 = humidity * humidity;
#if 1
/*
* A really precise formula. Note that overall precision is
* constrained by the accuracy of the instruments and that the
* we receive the temperature and dewpoints as integers.
*/
t3 = t2 * temp;
h3 = h2 * temp;
apparent = 16.923 + 0.185212 * temp + 5.37941 * humidity - 0.100254 * temp * humidity + 9.41695e-3 * t2 + 7.28898e-3 * h2 + 3.45372e-4 * t2 * humidity - 8.14971e-4 * temp * h2 + 1.02102e-5 * t2 * h2 - 3.8646e-5 * t3 + 2.91583e-5 * h3 + 1.42721e-6 * t3 * humidity + 1.97483e-7 * temp * h3 - 2.18429e-8 * t3 * h2 + 8.43296e-10 * t2 * h3 - 4.81975e-11 * t3 * h3;
#else
/*
* An often cited alternative: values are within 5 degrees for
* most ranges between 10% and 70% humidity and to 110 degrees.
*/
apparent = -42.379 + 2.04901523 * temp + 10.14333127 * humidity - 0.22475541 * temp * humidity - 6.83783e-3 * t2 - 5.481717e-2 * h2 + 1.22874e-3 * t2 * humidity + 8.5282e-4 * temp * h2 - 1.99e-6 * t2 * h2;
#endif
}
} else {
apparent = temp;
}
return apparent;
}
static void
gweather_info_reset (GWeatherInfo *info)
{
g_clear_pointer (&info->forecast_attribution, g_free);
free_forecast_list (info);
info->update = 0;
info->current_time = time (NULL);
info->sky = -1;
info->cond.significant = FALSE;
info->cond.phenomenon = GWEATHER_PHENOMENON_NONE;
info->cond.qualifier = GWEATHER_QUALIFIER_NONE;
info->temp = -1000.0;
info->tempMinMaxValid = FALSE;
info->temp_min = -1000.0;
info->temp_max = -1000.0;
info->dew = -1000.0;
info->humidity = -1.0;
info->wind = -1;
info->windspeed = -1;
info->pressure = -1.0;
info->visibility = -1.0;
info->sunriseValid = FALSE;
info->sunsetValid = FALSE;
info->moonValid = FALSE;
info->sunrise = 0;
info->sunset = 0;
info->moonphase = 0;
info->moonlatitude = 0;
info->forecast_list = NULL;
}
static void
settings_changed_cb (GSettings *settings,
const char *key,
GWeatherInfo *info)
{
/* Only emit the signal if no network requests are pending.
Otherwise just wait for the update that will happen at
the end
*/
if (info->requests_pending == NULL)
g_signal_emit (info, gweather_info_signals[SIGNAL_UPDATED], 0);
}
void
gweather_info_init (GWeatherInfo *info)
{
info->providers = GWEATHER_PROVIDER_METAR | GWEATHER_PROVIDER_IWIN;
info->settings = g_settings_new ("org.gnome.GWeather4");
g_signal_connect_object (info->settings, "changed", G_CALLBACK (settings_changed_cb), info, 0);
gweather_info_reset (info);
}
static SoupCache *
get_cache (void)
{
SoupCache *cache;
char *filename;
filename = g_build_filename (g_get_user_cache_dir (),
"libgweather",
NULL);
if (g_mkdir_with_parents (filename, 0700) < 0) {
g_warning ("Failed to create libgweather cache directory: %s. Check your XDG_CACHE_HOME setting!", strerror (errno));
g_free (filename);
return NULL;
}
cache = soup_cache_new (filename, SOUP_CACHE_SINGLE_USER);
g_free (filename);
return cache;
}
static void
dump_and_unref_cache (SoupCache *cache)
{
soup_cache_dump (cache);
g_object_unref (cache);
}
static SoupSession *static_session;
static SoupSession *
ref_session (GWeatherInfo *info)
{
SoupSession *session;
SoupCache *cache;
g_autofree char *user_agent = NULL;
session = static_session;
if (session != NULL)
return g_object_ref (session);
session = soup_session_new ();
user_agent = g_strdup_printf ("libgweather/%s %s (%s)",
LIBGWEATHER_VERSION,
info->application_id,
info->contact_info);
g_object_set (G_OBJECT (session), "user-agent", user_agent, NULL);
cache = get_cache ();
if (cache != NULL) {
soup_session_add_feature (session, SOUP_SESSION_FEATURE (cache));
g_object_set_data_full (G_OBJECT (session), "libgweather-cache", g_object_ref (cache), (GDestroyNotify) dump_and_unref_cache);
soup_cache_load (cache);
g_object_unref (cache);
}
static_session = session;
g_object_add_weak_pointer (G_OBJECT (session), (void **) &static_session);
return session;
}
/**
* gweather_info_store_cache:
*
* Ensures that any data cached from the network is stored to disk.
* Calling this is not necessary, as the cache will be saved when
* the last reference to a #GWeatherInfo will be dropped.
* On the other hand, it must be called if there is any chance that
* the application will be closed without unreffing all objects, such
* as when using a language binding that employs a GC.
*/
void
gweather_info_store_cache (void)
{
SoupCache *cache;
if (static_session == NULL)
return;
cache = g_object_get_data (G_OBJECT (static_session), "libgweather-cache");
soup_cache_dump (cache);
}
/**
* gweather_info_update:
* @info: a #GWeatherInfo
*
* Requests a reload of weather conditions and forecast data from
* enabled network services.
* This call does no synchronous IO: rather, the result is delivered
* by emitting the #GWeatherInfo::updated signal.
* Note that if no network services are enabled, the signal will not
* be emitted. See #GWeatherInfo:enabled-providers for details.
*/
void
gweather_info_update (GWeatherInfo *info)
{
gboolean ok;
if (info->providers == GWEATHER_PROVIDER_NONE)
return;
g_return_if_fail (info->application_id != NULL);
g_return_if_fail (g_application_id_is_valid (info->application_id));
g_return_if_fail (info->contact_info != NULL);
/* Update in progress */
if (!requests_init (info))
return;
gweather_info_reset (info);
if (!info->session)
info->session = ref_session (info);
if (info->providers & GWEATHER_PROVIDER_METAR)
metar_start_open (info);
ok = FALSE;
/* Try national forecast services first */
if (info->providers & GWEATHER_PROVIDER_IWIN)
ok = iwin_start_open (info);
if (ok)
return;
/* Try met.no next */
if (info->providers & GWEATHER_PROVIDER_MET_NO)
ok = metno_start_open (info);
if (ok)
return;
/* Try OpenWeatherMap next */
if (info->providers & GWEATHER_PROVIDER_OWM)
owm_start_open (info);
}
void
gweather_info_abort (GWeatherInfo *info)
{
GSList *list, *iter;
GSList dummy = { NULL, NULL };
g_return_if_fail (GWEATHER_IS_INFO (info));
if (info->session == NULL) {
g_assert (info->requests_pending == NULL);
return;
}
list = info->requests_pending;
/* to block updated signals */
info->requests_pending = &dummy;
for (iter = list; iter; iter = iter->next) {
#if SOUP_CHECK_VERSION (2, 99, 2)
g_cancellable_cancel (g_object_get_data (iter->data, "request-cancellable"));
#else
soup_session_cancel_message (info->session, iter->data, SOUP_STATUS_CANCELLED);
#endif
}
g_slist_free (list);
info->requests_pending = NULL;
}
static void
gweather_info_dispose (GObject *object)
{
GWeatherInfo *info = GWEATHER_INFO (object);
gweather_info_abort (info);
g_clear_object (&info->session);
free_forecast_list (info);
info->valid = FALSE;
G_OBJECT_CLASS (gweather_info_parent_class)->dispose (object);
}
static void
gweather_info_finalize (GObject *object)
{
GWeatherInfo *info = GWEATHER_INFO (object);
_weather_location_free (&info->location);
g_clear_object (&info->settings);
if (info->glocation)
gweather_location_unref (info->glocation);
g_clear_pointer (&info->application_id, g_free);
g_clear_pointer (&info->contact_info, g_free);
g_free (info->forecast_attribution);
G_OBJECT_CLASS (gweather_info_parent_class)->finalize (object);
}
gboolean
gweather_info_is_valid (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
return info->valid;
}
gboolean
gweather_info_network_error (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
return info->network_error;
}
const GWeatherLocation *
gweather_info_get_location (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
return info->glocation;
}
gchar *
gweather_info_get_location_name (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
return g_strdup (info->location.name);
}
gchar *
gweather_info_get_update (GWeatherInfo *info)
{
char *out;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
if (info->update != 0) {
GDateTime *now = g_date_time_new_from_unix_local (info->update);
out = g_date_time_format (now, _ ("%a, %b %d / %H∶%M"));
if (!out)
out = g_strdup ("???");
g_date_time_unref (now);
} else
out = g_strdup (_ ("Unknown observation time"));
return out;
}
gchar *
gweather_info_get_sky (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
if (info->sky < 0)
return g_strdup (C_ ("sky conditions", "Unknown"));
return g_strdup (gweather_sky_to_string (info->sky));
}
gchar *
gweather_info_get_conditions (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
return g_strdup (gweather_conditions_to_string (&info->cond));
}
static gboolean
is_locale_metric (void)
{
/* Translate to the default units to use for presenting
* lengths to the user. Translate to default:inch if you
* want inches, otherwise translate to default:mm.
* Do *not* translate it to "predefinito:mm", if it
* it isn't default:mm or default:inch it will not work
*/
gchar *e = _ ("default:mm");
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
const char *fmt;
fmt = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
if (fmt && *fmt == 2)
return FALSE;
else
return TRUE;
#endif
if (strcmp (e, "default:inch") == 0)
return FALSE;
else if (strcmp (e, "default:mm"))
g_warning ("Whoever translated default:mm did so wrongly.\n");
return TRUE;
}
/**
* gweather_temperature_unit_to_real:
* @unit: a tempeature unit, or %GWEATHER_TEMP_UNIT_DEFAULT
*
* Resolve @unit into a real temperature unit, potentially considering
* locale defaults.
*/
GWeatherTemperatureUnit
gweather_temperature_unit_to_real (GWeatherTemperatureUnit unit)
{
if (G_UNLIKELY (unit == GWEATHER_TEMP_UNIT_INVALID)) {
g_critical ("Conversion to invalid temperature unit");
unit = GWEATHER_TEMP_UNIT_DEFAULT;
}
if (unit == GWEATHER_TEMP_UNIT_DEFAULT)
return is_locale_metric () ? GWEATHER_TEMP_UNIT_CENTIGRADE : GWEATHER_TEMP_UNIT_FAHRENHEIT;
return unit;
}
static gchar *
temperature_string (gfloat temp_f, GWeatherTemperatureUnit to_unit, gboolean want_round)
{
to_unit = gweather_temperature_unit_to_real (to_unit);
switch (to_unit) {
case GWEATHER_TEMP_UNIT_FAHRENHEIT:
if (!want_round) {
/* TRANSLATOR: This is the temperature in degrees Fahrenheit
* with a non-break space (U+00A0) between the digits and the
* degrees sign (U+00B0), followed by the letter F
*/
return g_strdup_printf (_ ("%.1f\u00A0\u00B0F"), temp_f);
} else {
/* TRANSLATOR: This is the temperature in degrees Fahrenheit
* with a non-break space (U+00A0) between the digits and the
* degrees sign (U+00B0), followed by the letter F
*/
return g_strdup_printf (_ ("%d\u00A0\u00B0F"), (int) floor (temp_f + 0.5));
}
break;
case GWEATHER_TEMP_UNIT_CENTIGRADE:
if (!want_round) {
/* TRANSLATOR: This is the temperature in degrees Celsius
* with a non-break space (U+00A0) between the digits and
* the degrees sign (U+00B0), followed by the letter C
*/
return g_strdup_printf (_ ("%.1f\u00A0\u00B0C"), TEMP_F_TO_C (temp_f));
} else {
/* TRANSLATOR: This is the temperature in degrees Celsius
* with a non-break space (U+00A0) between the digits and
* the degrees sign (U+00B0), followed by the letter C
*/
return g_strdup_printf (_ ("%d\u00A0\u00B0C"), (int) floor (TEMP_F_TO_C (temp_f) + 0.5));
}
break;
case GWEATHER_TEMP_UNIT_KELVIN:
if (!want_round) {
/* TRANSLATOR: This is the temperature in kelvin (U+212A KELVIN SIGN)
* with a non-break space (U+00A0) between the digits and the degrees
* sign
*/
return g_strdup_printf (_ ("%.1f\u00A0\u212A"), TEMP_F_TO_K (temp_f));
} else {
/* TRANSLATOR: This is the temperature in kelvin (U+212A KELVIN SIGN)
* with a non-break space (U+00A0) between the digits and the degrees
* sign
*/
return g_strdup_printf (_ ("%d\u00A0\u212A"), (int) floor (TEMP_F_TO_K (temp_f)));
}
break;
case GWEATHER_TEMP_UNIT_INVALID:
case GWEATHER_TEMP_UNIT_DEFAULT:
g_assert_not_reached ();
}
return NULL;
}
gchar *
gweather_info_get_temp (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
if (info->temp < -500.0)
return g_strdup (C_ ("temperature", "Unknown"));
return temperature_string (info->temp, g_settings_get_enum (info->settings, TEMPERATURE_UNIT), FALSE);
}
gchar *
gweather_info_get_temp_min (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid || !info->tempMinMaxValid)
return g_strdup ("-");
if (info->temp_min < -500.0)
return g_strdup (C_ ("temperature", "Unknown"));
return temperature_string (info->temp_min, g_settings_get_enum (info->settings, TEMPERATURE_UNIT), FALSE);
}
gchar *
gweather_info_get_temp_max (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid || !info->tempMinMaxValid)
return g_strdup ("-");
if (info->temp_max < -500.0)
return g_strdup (C_ ("temperature", "Unknown"));
return temperature_string (info->temp_max, g_settings_get_enum (info->settings, TEMPERATURE_UNIT), FALSE);
}
gchar *
gweather_info_get_dew (GWeatherInfo *info)
{
gdouble dew;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
if (info->hasHumidity)
dew = calc_dew (info->temp, info->humidity);
else
dew = info->dew;
if (dew < -500.0)
return g_strdup (C_ ("dew", "Unknown"));
return temperature_string (info->dew, g_settings_get_enum (info->settings, TEMPERATURE_UNIT), FALSE);
}
gchar *
gweather_info_get_humidity (GWeatherInfo *info)
{
gdouble humidity;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
if (info->hasHumidity)
humidity = info->humidity;
else
humidity = calc_humidity (info->temp, info->dew);
if (humidity < 0.0)
return g_strdup (C_ ("humidity", "Unknown"));
/* TRANSLATOR: This is the humidity in percent */
return g_strdup_printf (_ ("%.f%%"), humidity);
}
gchar *
gweather_info_get_apparent (GWeatherInfo *info)
{
gdouble apparent;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
apparent = calc_apparent (info);
if (apparent < -500.0)
return g_strdup (C_ ("temperature", "Unknown"));
return temperature_string (apparent, g_settings_get_enum (info->settings, TEMPERATURE_UNIT), FALSE);
}
static GWeatherSpeedUnit
speed_unit_to_real (GWeatherSpeedUnit unit)
{
if (G_UNLIKELY (unit == GWEATHER_SPEED_UNIT_INVALID)) {
g_critical ("Conversion to invalid speed unit");
unit = GWEATHER_SPEED_UNIT_DEFAULT;
}
if (unit == GWEATHER_SPEED_UNIT_DEFAULT)
return is_locale_metric () ? GWEATHER_SPEED_UNIT_KPH : GWEATHER_SPEED_UNIT_MPH;
return unit;
}
static gchar *
windspeed_string (gfloat knots, GWeatherSpeedUnit to_unit)
{
to_unit = speed_unit_to_real (to_unit);
switch (to_unit) {
case GWEATHER_SPEED_UNIT_KNOTS:
/* TRANSLATOR: This is the wind speed in knots */
return g_strdup_printf (_ ("%0.1f knots"), knots);
case GWEATHER_SPEED_UNIT_MPH:
/* TRANSLATOR: This is the wind speed in miles per hour */
return g_strdup_printf (_ ("%.1f mph"), WINDSPEED_KNOTS_TO_MPH (knots));
case GWEATHER_SPEED_UNIT_KPH:
/* TRANSLATOR: This is the wind speed in kilometers per hour */
return g_strdup_printf (_ ("%.1f km/h"), WINDSPEED_KNOTS_TO_KPH (knots));
case GWEATHER_SPEED_UNIT_MS:
/* TRANSLATOR: This is the wind speed in meters per second */
return g_strdup_printf (_ ("%.1f m/s"), WINDSPEED_KNOTS_TO_MS (knots));
case GWEATHER_SPEED_UNIT_BFT:
/* TRANSLATOR: This is the wind speed as a Beaufort force factor
* (commonly used in nautical wind estimation).
*/
return g_strdup_printf (_ ("Beaufort force %.1f"), WINDSPEED_KNOTS_TO_BFT (knots));
case GWEATHER_SPEED_UNIT_INVALID:
case GWEATHER_SPEED_UNIT_DEFAULT:
g_assert_not_reached ();
}
return NULL;
}
gchar *
gweather_info_get_wind (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
if (info->windspeed < 0.0 || info->wind < 0)
return g_strdup (C_ ("wind speed", "Unknown"));
if (info->windspeed == 0.00) {
return g_strdup (_ ("Calm"));
} else {
gchar *speed_string;
gchar *wind_string;
speed_string = windspeed_string (info->windspeed, g_settings_get_enum (info->settings, SPEED_UNIT));
/* TRANSLATOR: This is 'wind direction' / 'wind speed' */
wind_string = g_strdup_printf (_ ("%s / %s"), gweather_wind_direction_to_string (info->wind), speed_string);
g_free (speed_string);
return wind_string;
}
}
/**
* gweather_speed_unit_to_string:
* @unit: a speed unit, or %GWEATHER_SPEED_UNIT_DEFAULT
*
* Creates a human-readable, localized representation of @unit
*/
const gchar *
gweather_speed_unit_to_string (GWeatherSpeedUnit unit)
{
unit = speed_unit_to_real (unit);
switch (unit) {
case GWEATHER_SPEED_UNIT_KNOTS:
/* TRANSLATOR: This is the wind speed in knots */
return _ ("knots");
case GWEATHER_SPEED_UNIT_MPH:
/* TRANSLATOR: This is the wind speed in miles per hour */
return _ ("mph");
case GWEATHER_SPEED_UNIT_KPH:
/* TRANSLATOR: This is the wind speed in kilometers per hour */
return _ ("km/h");
case GWEATHER_SPEED_UNIT_MS:
/* TRANSLATOR: This is the wind speed in meters per second */
return _ ("m/s");
case GWEATHER_SPEED_UNIT_BFT:
/* TRANSLATOR: This is the wind speed as a Beaufort force factor
* (commonly used in nautical wind estimation).
*/
return _ ("Beaufort force");
case GWEATHER_SPEED_UNIT_INVALID:
case GWEATHER_SPEED_UNIT_DEFAULT:
g_assert_not_reached ();
}
return NULL;
}
static GWeatherPressureUnit
pressure_unit_to_real (GWeatherPressureUnit unit)
{
if (G_UNLIKELY (unit == GWEATHER_PRESSURE_UNIT_INVALID)) {
g_critical ("Conversion to invalid pressure unit");
unit = GWEATHER_PRESSURE_UNIT_DEFAULT;
}
if (unit == GWEATHER_PRESSURE_UNIT_DEFAULT)
return is_locale_metric () ? GWEATHER_PRESSURE_UNIT_MM_HG : GWEATHER_PRESSURE_UNIT_INCH_HG;
return unit;
}
gchar *
gweather_info_get_pressure (GWeatherInfo *info)
{
GWeatherPressureUnit unit;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
if (info->pressure < 0.0)
return g_strdup (C_ ("pressure", "Unknown"));
unit = pressure_unit_to_real (g_settings_get_enum (info->settings, PRESSURE_UNIT));
switch (unit) {
case GWEATHER_PRESSURE_UNIT_INCH_HG:
/* TRANSLATOR: This is pressure in inches of mercury */
return g_strdup_printf (_ ("%.2f inHg"), info->pressure);
case GWEATHER_PRESSURE_UNIT_MM_HG:
/* TRANSLATOR: This is pressure in millimeters of mercury */
return g_strdup_printf (_ ("%.1f mmHg"), PRESSURE_INCH_TO_MM (info->pressure));
case GWEATHER_PRESSURE_UNIT_KPA:
/* TRANSLATOR: This is pressure in kiloPascals */
return g_strdup_printf (_ ("%.2f kPa"), PRESSURE_INCH_TO_KPA (info->pressure));
case GWEATHER_PRESSURE_UNIT_HPA:
/* TRANSLATOR: This is pressure in hectoPascals */
return g_strdup_printf (_ ("%.2f hPa"), PRESSURE_INCH_TO_HPA (info->pressure));
case GWEATHER_PRESSURE_UNIT_MB:
/* TRANSLATOR: This is pressure in millibars */
return g_strdup_printf (_ ("%.2f mb"), PRESSURE_INCH_TO_MB (info->pressure));
case GWEATHER_PRESSURE_UNIT_ATM:
/* TRANSLATOR: This is pressure in atmospheres */
return g_strdup_printf (_ ("%.3f atm"), PRESSURE_INCH_TO_ATM (info->pressure));
case GWEATHER_PRESSURE_UNIT_INVALID:
case GWEATHER_PRESSURE_UNIT_DEFAULT:
g_assert_not_reached ();
}
return NULL;
}
static GWeatherDistanceUnit
distance_unit_to_real (GWeatherDistanceUnit unit)
{
if (G_UNLIKELY (unit == GWEATHER_DISTANCE_UNIT_INVALID)) {
g_critical ("Conversion to invalid distance unit");
unit = GWEATHER_DISTANCE_UNIT_DEFAULT;
}
if (unit == GWEATHER_DISTANCE_UNIT_DEFAULT)
return is_locale_metric () ? GWEATHER_DISTANCE_UNIT_METERS : GWEATHER_DISTANCE_UNIT_MILES;
return unit;
}
gchar *
gweather_info_get_visibility (GWeatherInfo *info)
{
GWeatherDistanceUnit unit;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup ("-");
if (info->visibility < 0.0)
return g_strdup (C_ ("visibility", "Unknown"));
unit = distance_unit_to_real (g_settings_get_enum (info->settings, DISTANCE_UNIT));
switch (unit) {
case GWEATHER_DISTANCE_UNIT_MILES:
/* TRANSLATOR: This is the visibility in miles */
return g_strdup_printf (_ ("%.1f miles"), info->visibility);
case GWEATHER_DISTANCE_UNIT_KM:
/* TRANSLATOR: This is the visibility in kilometers */
return g_strdup_printf (_ ("%.1f km"), VISIBILITY_SM_TO_KM (info->visibility));
case GWEATHER_DISTANCE_UNIT_METERS:
/* TRANSLATOR: This is the visibility in meters */
return g_strdup_printf (_ ("%.0fm"), VISIBILITY_SM_TO_M (info->visibility));
case GWEATHER_DISTANCE_UNIT_INVALID:
case GWEATHER_DISTANCE_UNIT_DEFAULT:
g_assert_not_reached ();
}
return NULL;
}
gchar *
gweather_info_get_sunrise (GWeatherInfo *info)
{
GDateTime *sunrise;
gchar *buf;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
_gweather_info_ensure_sun (info);
if (!info->sunriseValid)
return g_strdup ("-");
sunrise = g_date_time_new_from_unix_local (info->sunrise);
buf = g_date_time_format (sunrise, _ ("%H∶%M"));
if (!buf)
buf = g_strdup ("-");
g_date_time_unref (sunrise);
return buf;
}
gchar *
gweather_info_get_sunset (GWeatherInfo *info)
{
GDateTime *sunset;
gchar *buf;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
_gweather_info_ensure_sun (info);
if (!info->sunsetValid)
return g_strdup ("-");
sunset = g_date_time_new_from_unix_local (info->sunset);
buf = g_date_time_format (sunset, _ ("%H∶%M"));
if (!buf)
buf = g_strdup ("-");
g_date_time_unref (sunset);
return buf;
}
/**
* gweather_info_get_forecast_list:
* @info: a #GWeatherInfo
*
* Returns: (transfer none) (element-type GWeather.Info): list
* of GWeatherInfo* objects for the forecast.
* The list is owned by the 'info' object thus is alive as long
* as the 'info'. The 'update' property is the date/time when the
* forecast info is used for.
**/
GSList *
gweather_info_get_forecast_list (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return NULL;
return info->forecast_list;
}
/**
* gweather_info_get_attribution:
* @info: a #GWeatherInfo
*
* Some weather services require the application showing the
* data to include an attribution text, possibly including links
* to the service website.
* This must be shown prominently toghether with the data.
*
* Returns: (transfer none): the required attribution text, in Pango
* markup form, or %NULL if not required
*/
const gchar *
gweather_info_get_attribution (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
return info->forecast_attribution;
}
gchar *
gweather_info_get_temp_summary (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid || info->temp < -500.0)
return g_strdup ("--");
return temperature_string (info->temp, g_settings_get_enum (info->settings, TEMPERATURE_UNIT), TRUE);
}
/**
* gweather_info_get_weather_summary:
* @info: a #GWeatherInfo
*
* Returns: (transfer full): a summary for current weather conditions.
*/
gchar *
gweather_info_get_weather_summary (GWeatherInfo *info)
{
gchar *buf;
gchar *out;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
if (!info->valid)
return g_strdup (_ ("Retrieval failed"));
buf = gweather_info_get_conditions (info);
if (g_str_equal (buf, "-")) {
g_free (buf);
buf = gweather_info_get_sky (info);
}
out = g_strdup_printf ("%s: %s", info->location.name, buf);
g_free (buf);
return out;
}
/**
* gweather_info_is_daytime:
* @info: a #GWeatherInfo
*
* Returns: Whether it is daytime (that is, if the sun is visible)
* or not at the location and the point of time referred by @info.
* This is mostly equivalent to comparing the return value
* of gweather_info_get_value_sunrise() and
* gweather_info_get_value_sunset(), but it accounts also
* for midnight sun and polar night, for locations within
* the Artic and Antartic circles.
*/
gboolean
gweather_info_is_daytime (GWeatherInfo *info)
{
time_t current_time;
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
_gweather_info_ensure_sun (info);
if (info->polarNight)
return FALSE;
if (info->midnightSun)
return TRUE;
current_time = info->current_time;
return (!info->sunriseValid || (current_time >= info->sunrise)) &&
(!info->sunsetValid || (current_time < info->sunset));
}
const gchar *
gweather_info_get_icon_name (GWeatherInfo *info)
{
GWeatherConditions cond;
GWeatherSky sky;
gboolean daytime;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
cond = info->cond;
sky = info->sky;
if (cond.significant) {
if (cond.phenomenon != GWEATHER_PHENOMENON_NONE &&
cond.qualifier == GWEATHER_QUALIFIER_THUNDERSTORM)
return "weather-storm";
switch (cond.phenomenon) {
case GWEATHER_PHENOMENON_INVALID:
case GWEATHER_PHENOMENON_LAST:
case GWEATHER_PHENOMENON_NONE:
break;
case GWEATHER_PHENOMENON_DRIZZLE:
case GWEATHER_PHENOMENON_RAIN:
case GWEATHER_PHENOMENON_UNKNOWN_PRECIPITATION:
case GWEATHER_PHENOMENON_HAIL:
case GWEATHER_PHENOMENON_SMALL_HAIL:
return "weather-showers";
case GWEATHER_PHENOMENON_SNOW:
case GWEATHER_PHENOMENON_SNOW_GRAINS:
case GWEATHER_PHENOMENON_ICE_PELLETS:
case GWEATHER_PHENOMENON_ICE_CRYSTALS:
return "weather-snow";
case GWEATHER_PHENOMENON_TORNADO:
case GWEATHER_PHENOMENON_SQUALL:
return "weather-storm";
case GWEATHER_PHENOMENON_MIST:
case GWEATHER_PHENOMENON_FOG:
case GWEATHER_PHENOMENON_SMOKE:
case GWEATHER_PHENOMENON_VOLCANIC_ASH:
case GWEATHER_PHENOMENON_SAND:
case GWEATHER_PHENOMENON_HAZE:
case GWEATHER_PHENOMENON_SPRAY:
case GWEATHER_PHENOMENON_DUST:
case GWEATHER_PHENOMENON_SANDSTORM:
case GWEATHER_PHENOMENON_DUSTSTORM:
case GWEATHER_PHENOMENON_FUNNEL_CLOUD:
case GWEATHER_PHENOMENON_DUST_WHIRLS:
return "weather-fog";
}
}
daytime = gweather_info_is_daytime (info);
switch (sky) {
case GWEATHER_SKY_INVALID:
case GWEATHER_SKY_LAST:
case GWEATHER_SKY_CLEAR:
if (daytime)
return "weather-clear";
else
return "weather-clear-night";
case GWEATHER_SKY_BROKEN:
case GWEATHER_SKY_SCATTERED:
case GWEATHER_SKY_FEW:
if (daytime)
return "weather-few-clouds";
else
return "weather-few-clouds-night";
case GWEATHER_SKY_OVERCAST:
return "weather-overcast";
default: /* unrecognized */
return NULL;
}
}
const gchar *
gweather_info_get_symbolic_icon_name (GWeatherInfo *info)
{
GWeatherConditions cond;
GWeatherSky sky;
gboolean daytime;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
cond = info->cond;
sky = info->sky;
if (cond.significant) {
if (cond.phenomenon != GWEATHER_PHENOMENON_NONE &&
cond.qualifier == GWEATHER_QUALIFIER_THUNDERSTORM)
return "weather-storm-symbolic";
switch (cond.phenomenon) {
case GWEATHER_PHENOMENON_INVALID:
case GWEATHER_PHENOMENON_LAST:
case GWEATHER_PHENOMENON_NONE:
break;
case GWEATHER_PHENOMENON_DRIZZLE:
case GWEATHER_PHENOMENON_RAIN:
case GWEATHER_PHENOMENON_UNKNOWN_PRECIPITATION:
case GWEATHER_PHENOMENON_HAIL:
case GWEATHER_PHENOMENON_SMALL_HAIL:
return "weather-showers-symbolic";
case GWEATHER_PHENOMENON_SNOW:
case GWEATHER_PHENOMENON_SNOW_GRAINS:
case GWEATHER_PHENOMENON_ICE_PELLETS:
case GWEATHER_PHENOMENON_ICE_CRYSTALS:
return "weather-snow-symbolic";
case GWEATHER_PHENOMENON_TORNADO:
case GWEATHER_PHENOMENON_SQUALL:
return "weather-storm-symbolic";
case GWEATHER_PHENOMENON_MIST:
case GWEATHER_PHENOMENON_FOG:
case GWEATHER_PHENOMENON_SMOKE:
case GWEATHER_PHENOMENON_VOLCANIC_ASH:
case GWEATHER_PHENOMENON_SAND:
case GWEATHER_PHENOMENON_HAZE:
case GWEATHER_PHENOMENON_SPRAY:
case GWEATHER_PHENOMENON_DUST:
case GWEATHER_PHENOMENON_SANDSTORM:
case GWEATHER_PHENOMENON_DUSTSTORM:
case GWEATHER_PHENOMENON_FUNNEL_CLOUD:
case GWEATHER_PHENOMENON_DUST_WHIRLS:
return "weather-fog-symbolic";
}
}
daytime = gweather_info_is_daytime (info);
switch (sky) {
case GWEATHER_SKY_INVALID:
case GWEATHER_SKY_LAST:
case GWEATHER_SKY_CLEAR:
if (daytime)
return "weather-clear-symbolic";
else
return "weather-clear-night-symbolic";
case GWEATHER_SKY_BROKEN:
case GWEATHER_SKY_SCATTERED:
case GWEATHER_SKY_FEW:
if (daytime)
return "weather-few-clouds-symbolic";
else
return "weather-few-clouds-night-symbolic";
case GWEATHER_SKY_OVERCAST:
return "weather-overcast-symbolic";
default: /* unrecognized */
return NULL;
}
}
static gboolean
temperature_value (gdouble temp_f,
GWeatherTemperatureUnit to_unit,
gdouble *value,
GSettings *settings)
{
gboolean ok = TRUE;
*value = 0.0;
if (temp_f < -500.0)
return FALSE;
if (to_unit == GWEATHER_TEMP_UNIT_DEFAULT)
to_unit = g_settings_get_enum (settings, TEMPERATURE_UNIT);
to_unit = gweather_temperature_unit_to_real (to_unit);
switch (to_unit) {
case GWEATHER_TEMP_UNIT_FAHRENHEIT:
*value = temp_f;
break;
case GWEATHER_TEMP_UNIT_CENTIGRADE:
*value = TEMP_F_TO_C (temp_f);
break;
case GWEATHER_TEMP_UNIT_KELVIN:
*value = TEMP_F_TO_K (temp_f);
break;
case GWEATHER_TEMP_UNIT_INVALID:
case GWEATHER_TEMP_UNIT_DEFAULT:
g_assert_not_reached ();
}
return ok;
}
static gboolean
speed_value (gdouble knots,
GWeatherSpeedUnit to_unit,
gdouble *value,
GSettings *settings)
{
gboolean ok = TRUE;
*value = -1.0;
if (knots < 0.0)
return FALSE;
if (to_unit == GWEATHER_SPEED_UNIT_DEFAULT)
to_unit = g_settings_get_enum (settings, SPEED_UNIT);
to_unit = speed_unit_to_real (to_unit);
switch (to_unit) {
case GWEATHER_SPEED_UNIT_KNOTS:
*value = knots;
break;
case GWEATHER_SPEED_UNIT_MPH:
*value = WINDSPEED_KNOTS_TO_MPH (knots);
break;
case GWEATHER_SPEED_UNIT_KPH:
*value = WINDSPEED_KNOTS_TO_KPH (knots);
break;
case GWEATHER_SPEED_UNIT_MS:
*value = WINDSPEED_KNOTS_TO_MS (knots);
break;
case GWEATHER_SPEED_UNIT_BFT:
*value = WINDSPEED_KNOTS_TO_BFT (knots);
break;
case GWEATHER_SPEED_UNIT_INVALID:
case GWEATHER_SPEED_UNIT_DEFAULT:
g_assert_not_reached ();
}
return ok;
}
static gboolean
pressure_value (gdouble inHg,
GWeatherPressureUnit to_unit,
gdouble *value,
GSettings *settings)
{
gboolean ok = TRUE;
*value = -1.0;
if (inHg < 0.0)
return FALSE;
if (to_unit == GWEATHER_PRESSURE_UNIT_DEFAULT)
to_unit = g_settings_get_enum (settings, PRESSURE_UNIT);
to_unit = pressure_unit_to_real (to_unit);
switch (to_unit) {
case GWEATHER_PRESSURE_UNIT_INCH_HG:
*value = inHg;
break;
case GWEATHER_PRESSURE_UNIT_MM_HG:
*value = PRESSURE_INCH_TO_MM (inHg);
break;
case GWEATHER_PRESSURE_UNIT_KPA:
*value = PRESSURE_INCH_TO_KPA (inHg);
break;
case GWEATHER_PRESSURE_UNIT_HPA:
*value = PRESSURE_INCH_TO_HPA (inHg);
break;
case GWEATHER_PRESSURE_UNIT_MB:
*value = PRESSURE_INCH_TO_MB (inHg);
break;
case GWEATHER_PRESSURE_UNIT_ATM:
*value = PRESSURE_INCH_TO_ATM (inHg);
break;
case GWEATHER_PRESSURE_UNIT_INVALID:
case GWEATHER_PRESSURE_UNIT_DEFAULT:
g_assert_not_reached ();
}
return ok;
}
static gboolean
distance_value (gdouble miles,
GWeatherDistanceUnit to_unit,
gdouble *value,
GSettings *settings)
{
gboolean ok = TRUE;
*value = -1.0;
if (miles < 0.0)
return FALSE;
if (to_unit == GWEATHER_DISTANCE_UNIT_DEFAULT)
to_unit = g_settings_get_enum (settings, DISTANCE_UNIT);
to_unit = distance_unit_to_real (to_unit);
switch (to_unit) {
case GWEATHER_DISTANCE_UNIT_MILES:
*value = miles;
break;
case GWEATHER_DISTANCE_UNIT_KM:
*value = VISIBILITY_SM_TO_KM (miles);
break;
case GWEATHER_DISTANCE_UNIT_METERS:
*value = VISIBILITY_SM_TO_M (miles);
break;
case GWEATHER_DISTANCE_UNIT_INVALID:
case GWEATHER_DISTANCE_UNIT_DEFAULT:
g_assert_not_reached ();
}
return ok;
}
/**
* gweather_info_get_value_sky:
* @info: a #GWeatherInfo
* @sky: (out): a location for a #GWeatherSky.
*
* Fills out @sky with current sky conditions.
* Returns: TRUE is @sky is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_sky (GWeatherInfo *info, GWeatherSky *sky)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (sky != NULL, FALSE);
if (!info->valid)
return FALSE;
if (info->sky <= GWEATHER_SKY_INVALID || info->sky >= GWEATHER_SKY_LAST)
return FALSE;
*sky = info->sky;
return TRUE;
}
/**
* gweather_info_get_value_conditions:
* @info: a #GWeatherInfo
* @phenomenon: (out): a location for a #GWeatherConditionPhenomenon.
* @qualifier: (out): a location for a #GWeatherConditionQualifier.
*
* Fills out @phenomenon and @qualifier with current weather conditions.
* Returns: TRUE is out arguments are valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_conditions (GWeatherInfo *info, GWeatherConditionPhenomenon *phenomenon, GWeatherConditionQualifier *qualifier)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (phenomenon != NULL, FALSE);
g_return_val_if_fail (qualifier != NULL, FALSE);
if (!info->valid)
return FALSE;
if (!info->cond.significant)
return FALSE;
if (!(info->cond.phenomenon > GWEATHER_PHENOMENON_INVALID &&
info->cond.phenomenon < GWEATHER_PHENOMENON_LAST &&
info->cond.qualifier > GWEATHER_QUALIFIER_INVALID &&
info->cond.qualifier < GWEATHER_QUALIFIER_LAST))
return FALSE;
*phenomenon = info->cond.phenomenon;
*qualifier = info->cond.qualifier;
return TRUE;
}
/**
* gweather_info_get_value_temp:
* @info: a #GWeatherInfo
* @unit: the desired unit, as a #GWeatherTemperatureUnit
* @value: (out): the temperature value
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_temp (GWeatherInfo *info, GWeatherTemperatureUnit unit, gdouble *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
if (!info->valid)
return FALSE;
return temperature_value (info->temp, unit, value, info->settings);
}
/**
* gweather_info_get_value_temp_min:
* @info: a #GWeatherInfo
* @unit: the desired unit, as a #GWeatherTemperatureUnit
* @value: (out): the minimum temperature value
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_temp_min (GWeatherInfo *info, GWeatherTemperatureUnit unit, gdouble *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
if (!info->valid || !info->tempMinMaxValid)
return FALSE;
return temperature_value (info->temp_min, unit, value, info->settings);
}
/**
* gweather_info_get_value_temp_max:
* @info: a #GWeatherInfo
* @unit: the desired unit, as a #GWeatherTemperatureUnit
* @value: (out): the maximum temperature value
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_temp_max (GWeatherInfo *info, GWeatherTemperatureUnit unit, gdouble *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
if (!info->valid || !info->tempMinMaxValid)
return FALSE;
return temperature_value (info->temp_max, unit, value, info->settings);
}
/**
* gweather_info_get_value_dew:
* @info: a #GWeatherInfo
* @unit: the desired unit, as a #GWeatherTemperatureUnit
* @value: (out): the dew point
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_dew (GWeatherInfo *info, GWeatherTemperatureUnit unit, gdouble *value)
{
gdouble dew;
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
if (!info->valid)
return FALSE;
if (info->hasHumidity)
dew = calc_dew (info->temp, info->humidity);
else
dew = info->dew;
return temperature_value (dew, unit, value, info->settings);
}
/**
* gweather_info_get_value_apparent:
* @info: a #GWeatherInfo
* @unit: the desired unit, as a #GWeatherTemperatureUnit
* @value: (out): the apparent temperature
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_apparent (GWeatherInfo *info, GWeatherTemperatureUnit unit, gdouble *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
if (!info->valid)
return FALSE;
return temperature_value (calc_apparent (info), unit, value, info->settings);
}
/**
* gweather_info_get_value_update:
* @info: a #GWeatherInfo
* @value: (out) (type glong): the time @info was last updated
*
* Note that @value may be 0 if @info has not yet been updated.
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_update (GWeatherInfo *info, time_t *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
if (!info->valid)
return FALSE;
*value = info->update;
return TRUE;
}
/**
* gweather_info_get_value_sunrise:
* @info: a #GWeatherInfo
* @value: (out) (type gulong): the time of sunrise
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_sunrise (GWeatherInfo *info, time_t *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
_gweather_info_ensure_sun (info);
if (!info->sunriseValid)
return FALSE;
*value = info->sunrise;
return TRUE;
}
/**
* gweather_info_get_value_sunset:
* @info: a #GWeatherInfo
* @value: (out) (type gulong): the time of sunset
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_sunset (GWeatherInfo *info, time_t *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
_gweather_info_ensure_sun (info);
if (!info->sunsetValid)
return FALSE;
*value = info->sunset;
return TRUE;
}
/**
* gweather_info_get_value_moonphase:
* @info: a #GWeatherInfo
* @value: (out): the current moon phase
* @lat: (out): the moon declension
*
* Returns: TRUE is @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_moonphase (GWeatherInfo *info,
GWeatherMoonPhase *value,
GWeatherMoonLatitude *lat)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
g_return_val_if_fail (lat != NULL, FALSE);
_gweather_info_ensure_moon (info);
if (!info->moonValid)
return FALSE;
*value = info->moonphase;
*lat = info->moonlatitude;
return TRUE;
}
/**
* gweather_info_get_value_wind:
* @info: a #GWeatherInfo
* @unit: the desired unit, as a #GWeatherSpeedUnit
* @speed: (out): forecasted wind speed
* @direction: (out): forecasted wind direction
*
* Returns: TRUE if @speed and @direction are valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_wind (GWeatherInfo *info,
GWeatherSpeedUnit unit,
gdouble *speed,
GWeatherWindDirection *direction)
{
gboolean res = FALSE;
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (speed != NULL, FALSE);
g_return_val_if_fail (direction != NULL, FALSE);
if (!info->valid)
return FALSE;
if (info->windspeed < 0.0 || info->wind <= GWEATHER_WIND_INVALID || info->wind >= GWEATHER_WIND_LAST)
return FALSE;
res = speed_value (info->windspeed, unit, speed, info->settings);
*direction = info->wind;
return res;
}
/**
* gweather_info_get_value_pressure:
* @info: a #GWeatherInfo
* @unit: the desired unit, as a #GWeatherPressureUnit
* @value: (out): forecasted pressure, expressed in @unit
*
* Returns: TRUE if @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_pressure (GWeatherInfo *info,
GWeatherPressureUnit unit,
gdouble *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
if (!info->valid)
return FALSE;
return pressure_value (info->pressure, unit, value, info->settings);
}
/**
* gweather_info_get_value_visibility:
* @info: a #GWeatherInfo
* @unit: the desired unit, as a #GWeatherDistanceUnit
* @value: (out): forecasted visibility, expressed in @unit
*
* Returns: TRUE if @value is valid, FALSE otherwise.
*/
gboolean
gweather_info_get_value_visibility (GWeatherInfo *info,
GWeatherDistanceUnit unit,
gdouble *value)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
if (!info->valid)
return FALSE;
return distance_value (info->visibility, unit, value, info->settings);
}
static void
gweather_info_set_location_internal (GWeatherInfo *info,
GWeatherLocation *location)
{
GVariant *default_loc = NULL;
const gchar *name = NULL;
gboolean latlon_override = FALSE;
gdouble lat, lon;
if (info->glocation == location)
return;
if (info->glocation)
gweather_location_unref (info->glocation);
info->glocation = location;
if (info->glocation) {
gweather_location_ref (location);
} else {
g_autoptr (GWeatherLocation) world = NULL;
const gchar *station_code;
default_loc = g_settings_get_value (info->settings, DEFAULT_LOCATION);
g_variant_get (default_loc, "(&s&sm(dd))", &name, &station_code, &latlon_override, &lat, &lon);
if (strcmp (name, "") == 0)
name = NULL;
world = gweather_location_get_world ();
info->glocation = gweather_location_find_by_station_code (world, station_code);
}
if (info->glocation) {
_weather_location_free (&info->location);
_gweather_location_update_weather_location (info->glocation,
&info->location);
}
if (name) {
g_free (info->location.name);
info->location.name = g_strdup (name);
}
if (latlon_override) {
info->location.latlon_valid = TRUE;
info->location.latitude = DEGREES_TO_RADIANS (lat);
info->location.longitude = DEGREES_TO_RADIANS (lon);
}
if (default_loc)
g_variant_unref (default_loc);
}
/**
* gweather_info_set_location:
* @info: a #GWeatherInfo
* @location: (allow-none): a location for which weather is desired
*
* Changes @info to report weather for @location.
* Note that this will clear any forecast or current conditions from
* @info, you must call gweather_info_update() to obtain the new data.
*/
void
gweather_info_set_location (GWeatherInfo *info,
GWeatherLocation *location)
{
g_return_if_fail (GWEATHER_IS_INFO (info));
gweather_info_set_location_internal (info, location);
gweather_info_reset (info);
}
/**
* gweather_info_get_enabled_providers:
* @info: a #GWeatherInfo
*
* Gets the bitmask of enabled #GWeatherProvider weather
* providers.
*/
GWeatherProvider
gweather_info_get_enabled_providers (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info),
GWEATHER_PROVIDER_NONE);
return info->providers;
}
/**
* gweather_info_set_enabled_providers:
* @info: a #GWeatherInfo
* @providers: a bitmask of #GWeatherProvider
*
* Sets the enabled providers for fetching the weather. Note
* that it is up to the application developer to make sure that
* the terms of use for each service are respected.
*
* Online providers will not be enabled if the application ID is
* not set to a valid value.
*/
void
gweather_info_set_enabled_providers (GWeatherInfo *info,
GWeatherProvider providers)
{
g_return_if_fail (GWEATHER_IS_INFO (info));
if (providers != GWEATHER_PROVIDER_NONE) {
g_return_if_fail (info->application_id != NULL);
g_return_if_fail (g_application_id_is_valid (info->application_id));
}
if (info->providers == providers)
return;
info->providers = providers;
gweather_info_abort (info);
g_object_notify (G_OBJECT (info), "enabled-providers");
}
/**
* gweather_info_get_application_id:
* @info: a #GWeatherInfo
*
* Get the [application ID](https://docs.flatpak.org/en/latest/conventions.html#application-ids)
* of the application fetching the weather.
*
* Returns: the application ID
*/
const char *
gweather_info_get_application_id (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
return info->application_id;
}
/**
* gweather_info_set_application_id:
* @info: a #GWeatherInfo
* @application_id: the application ID to set
*
* Sets the [application ID](https://docs.flatpak.org/en/latest/conventions.html#application-ids)
* of the application fetching the weather. It is a requirement
* for using any of the online weather providers.
*
* If the application uses #GApplication, then the application ID
* will be automatically filled in.
*/
void
gweather_info_set_application_id (GWeatherInfo *info,
const char *application_id)
{
g_return_if_fail (GWEATHER_IS_INFO (info));
g_return_if_fail (g_application_id_is_valid (application_id));
g_clear_pointer (&info->application_id, g_free);
info->application_id = g_strdup (application_id);
if (info->session) {
g_clear_object (&info->session);
info->session = ref_session (info);
}
}
/**
* gweather_info_get_contact_info:
* @info: a #GWeatherInfo
*
* Get the contact information of the application fetching the weather.
*
* Returns: the contact information
*/
const char *
gweather_info_get_contact_info (GWeatherInfo *info)
{
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
return info->contact_info;
}
/**
* gweather_info_set_contact_info:
* @info: a #GWeatherInfo
* @contact_info: the contact information for the application
*
* Sets the contact information for the application fetching the
* weather. It is a requirement for using any of the online
* weather providers as it allows API providers to contact application
* developers in case of terms of use breaches.
*
* The contact information should be an email address, or the full
* URL to an online contact form which weather providers can use
* to contact the application developer. Avoid using bug tracker
* URLs which require creating accounts.
*/
void
gweather_info_set_contact_info (GWeatherInfo *info,
const char *contact_info)
{
g_return_if_fail (GWEATHER_IS_INFO (info));
g_return_if_fail (contact_info != NULL);
g_clear_pointer (&info->contact_info, g_free);
info->contact_info = g_strdup (contact_info);
if (info->session) {
g_clear_object (&info->session);
info->session = ref_session (info);
}
}
static void
gweather_info_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GWeatherInfo *self = GWEATHER_INFO (object);
switch (property_id) {
case PROP_LOCATION:
gweather_info_set_location_internal (self, (GWeatherLocation *) g_value_get_boxed (value));
break;
case PROP_ENABLED_PROVIDERS:
gweather_info_set_enabled_providers (self, g_value_get_flags (value));
break;
case PROP_APPLICATION_ID:
gweather_info_set_application_id (self, g_value_get_string (value));
break;
case PROP_CONTACT_INFO:
gweather_info_set_contact_info (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gweather_info_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GWeatherInfo *self = GWEATHER_INFO (object);
switch (property_id) {
case PROP_LOCATION:
g_value_set_boxed (value, self->glocation);
break;
case PROP_ENABLED_PROVIDERS:
g_value_set_flags (value, self->providers);
break;
case PROP_APPLICATION_ID:
g_value_set_string (value, self->application_id);
break;
case PROP_CONTACT_INFO:
g_value_set_string (value, self->contact_info);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gweather_info_constructed (GObject *object)
{
GWeatherInfo *info = GWEATHER_INFO (object);
GApplication *app;
if (info->application_id != NULL)
return;
app = g_application_get_default ();
if (!app)
return;
gweather_info_set_application_id (info,
g_application_get_application_id (app));
}
void
gweather_info_class_init (GWeatherInfoClass *klass)
{
GParamSpec *pspec;
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = gweather_info_dispose;
gobject_class->finalize = gweather_info_finalize;
gobject_class->set_property = gweather_info_set_property;
gobject_class->get_property = gweather_info_get_property;
gobject_class->constructed = gweather_info_constructed;
pspec = g_param_spec_boxed ("location",
"Location",
"The location this info represents",
GWEATHER_TYPE_LOCATION,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
g_object_class_install_property (gobject_class, PROP_LOCATION, pspec);
pspec = g_param_spec_flags ("enabled-providers",
"Enabled providers",
"A bitmask of enabled weather service providers",
GWEATHER_TYPE_PROVIDER,
GWEATHER_PROVIDER_NONE,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property (gobject_class, PROP_ENABLED_PROVIDERS, pspec);
pspec = g_param_spec_string ("application-id",
"Application ID",
"An unique reverse-DNS application ID",
NULL,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property (gobject_class, PROP_APPLICATION_ID, pspec);
pspec = g_param_spec_string ("contact-info",
"Contact information",
"An email address or contact form URL",
NULL,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property (gobject_class, PROP_CONTACT_INFO, pspec);
/**
* GWeatherInfo::updated:
* @object: the emitter of the signal.
*
* This signal is emitted after the initial fetch of the weather
* data from upstream services, and after every successful call
* to @gweather_info_update().
*/
gweather_info_signals[SIGNAL_UPDATED] = g_signal_new ("updated",
GWEATHER_TYPE_INFO,
G_SIGNAL_RUN_FIRST,
0,
NULL, /* accumulator */
NULL, /* accu_data */
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
_gweather_gettext_init ();
}
/**
* gweather_info_new:
* @location: (allow-none): the desidered #GWeatherLocation (%NULL for default)
*
* Builds a new #GWeatherInfo that will provide weather information about
* @location.
*
* Since version 40, this does not automatically call gweather_info_update().
* gweather_info_update() must be called manually once all the necessary set up
* has been made, such as setting the enabled providers.
*
* Returns: (transfer full): a new #GWeatherInfo
*/
GWeatherInfo *
gweather_info_new (GWeatherLocation *location)
{
if (location != NULL)
return g_object_new (GWEATHER_TYPE_INFO, "location", location, NULL);
return g_object_new (GWEATHER_TYPE_INFO, NULL);
}
GWeatherInfo *
_gweather_info_new_clone (GWeatherInfo *original)
{
return g_object_new (GWEATHER_TYPE_INFO,
"application-id",
original->application_id,
"contact-info",
original->contact_info,
"enabled-providers",
original->providers,
"location",
original->glocation,
NULL);
}
|