summaryrefslogtreecommitdiffstats
path: root/panels/wwan/cc-wwan-data.c
blob: 2608ab2248090d3a56cf92206d6db16bd3236d20 (plain)
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
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* cc-wwan-data.c
 *
 * Copyright 2019 Purism SPC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s):
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "cc-wwan-data"

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#define _GNU_SOURCE
#include <string.h>
#include <glib/gi18n.h>
#include <nma-mobile-providers.h>

#include "cc-wwan-data.h"

/**
 * @short_description: Device Internet Data Object
 * @include: "cc-wwan-device-data.h"
 *
 * #CcWwanData represents the data object of the given
 * #CcWwanDevice.  Please note that while #CcWWanDevice
 * is bound to the hardware device, #CcWwanData may also
 * depend on the inserted SIM (if supported). So the state
 * of #CcWwanData changes when SIM is changed.
 */

/*
 * Priority for connections. The larger the number, the lower the priority
 * https://developer.gnome.org/NetworkManager/stable/nm-settings.html:
 *
 *   A lower value is better (higher priority). Zero selects a globally
 *   configured default value. If the latter is missing or zero too, it
 *   defaults to 50 for VPNs and 100 for other connections.
 *
 * Since WiFi and other network connections will likely get the default
 * setting of 100, set WWAN DNS priorities higher than the default, with
 * room to allow multiple modems to set priority above/below each other.
 */
#define CC_WWAN_DNS_PRIORITY_LOW  (120)
#define CC_WWAN_DNS_PRIORITY_HIGH (115)

/* These are to be set as route metric */
#define CC_WWAN_ROUTE_PRIORITY_LOW  (1050)
#define CC_WWAN_ROUTE_PRIORITY_HIGH (1040)

struct _CcWwanData
{
  GObject      parent_instance;

  MMObject    *mm_object;
  MMModem     *modem;
  MMSim       *sim;
  gchar       *sim_id;

  gchar       *operator_code; /* MCCMNC */
  GError      *error;

  NMClient           *nm_client;
  NMDevice           *nm_device;
  NMAMobileProvidersDatabase *apn_db;
  NMAMobileProvider  *apn_provider;
  CcWwanDataApn      *default_apn;
  CcWwanDataApn      *old_default_apn;
  GListStore         *apn_list;
  NMActiveConnection *active_connection;

  gint     priority;
  gboolean data_enabled; /* autoconnect enabled */
  gboolean home_only;    /* Data roaming */
  gboolean apn_list_updated;    /* APN list updated from mobile-provider-info */
};

G_DEFINE_TYPE (CcWwanData, cc_wwan_data, G_TYPE_OBJECT)

/*
 * Default Access Point Settings Logic:
 * For a provided SIM, all the APNs available from NetworkManager
 * that matches the given SIM identifier (ICCID, available via
 * mm_sim_get_identifier() or similar gdbus API) is loaded for
 * the Device (In NetworkManager, it is saved as ‘sim-id’, if
 * present).  At a time, only one connection will be bound to
 * a device.  If there are more than one match, the item with
 * the highest ‘route-metric’ is taken.  If more matches are
 * still available, the first item is chosen.
 *
 * Populating All available APNs:
 * All Possible APNs for the given sim are populated the following
 * way (A list of all the following avoiding duplicates)
 * 1. The above mentioned “Default Access Point Settings Logic”
 * 2. Get All saved Network Manager connections with the
 *    provided MCCMNC of the given SIM
 * 3. Get All possible APNs for the MCCMNC from mobile-provider-info
 *
 * Testing if data is enabled:
 * Check if any of the items from step 1 have ‘autoconnect’ set
 *
 * Checking/Setting current SIM for data (in case of multiple SIM):
 * Since other networks (like wifi, ethernet) should have higher
 * priorities we use a negative number for priority.
 * 1. All APNs by default have priority CC_WWAN_APN_PRIORITY_LOW
 * 2. APN of selected SIM for active data have priority of
 *    CC_WWAN_APN_PRIORITY_HIGH
 *
 * XXX: Since users may create custom APNs via nmtui or like tools
 * we may have to check if there are some inconsistencies with APNs
 * available in NetworkManager, and ask user if they have to reset
 * the APNs that have invalid settings (basically, we care only APNs
 * that are set to have ‘autoconnect’ enabled, and all we need is to
 * disable autoconnect). We won’t interfere CDMA/EVDO networks.
 */
struct _CcWwanDataApn {
  GObject parent_instance;

  /* Set if the APN is from the mobile-provider-info database */
  NMAMobileAccessMethod *access_method;

  /* Set if the APN is saved in NetworkManager */
  NMConnection *nm_connection;
  NMRemoteConnection *remote_connection;

  gboolean modified;
};

G_DEFINE_TYPE (CcWwanDataApn, cc_wwan_data_apn, G_TYPE_OBJECT)

enum {
  PROP_0,
  PROP_ERROR,
  PROP_ENABLED,
  N_PROPS
};

static GParamSpec *properties[N_PROPS];

static void
wwan_data_apn_reset (CcWwanDataApn *apn)
{
  if (!apn)
    return;

  g_clear_object (&apn->nm_connection);
  g_clear_object (&apn->remote_connection);
}

static NMConnection *
wwan_data_get_nm_connection (CcWwanDataApn *apn)
{
  NMConnection *connection;
  NMSetting *setting;
  g_autofree gchar *uuid = NULL;

  if (apn->nm_connection)
    return apn->nm_connection;

  if (apn->remote_connection)
    return NM_CONNECTION (apn->remote_connection);

  connection = nm_simple_connection_new ();
  apn->nm_connection = connection;

  setting = nm_setting_connection_new ();
  uuid = nm_utils_uuid_generate ();
  g_object_set (setting,
                NM_SETTING_CONNECTION_UUID, uuid,
                NM_SETTING_CONNECTION_TYPE, NM_SETTING_GSM_SETTING_NAME,
                NULL);
  nm_connection_add_setting (connection, setting);

  setting = nm_setting_serial_new ();
  nm_connection_add_setting (connection, setting);

  setting = nm_setting_ip4_config_new ();
  g_object_set (setting, NM_SETTING_IP_CONFIG_METHOD, "auto", NULL);
  nm_connection_add_setting (connection, setting);

  nm_connection_add_setting (connection, nm_setting_gsm_new ());
  nm_connection_add_setting (connection, nm_setting_ppp_new ());

  return apn->nm_connection;
}

static gboolean
wwan_data_apn_are_same (CcWwanDataApn         *apn,
                        NMAMobileAccessMethod *access_method)
{
  NMConnection *connection;
  NMSetting *setting;

  if (!apn->remote_connection)
    return FALSE;

  connection = NM_CONNECTION (apn->remote_connection);
  setting = NM_SETTING (nm_connection_get_setting_gsm (connection));

  if (g_strcmp0 (nma_mobile_access_method_get_3gpp_apn (access_method),
                 nm_setting_gsm_get_apn (NM_SETTING_GSM (setting))) != 0)
    return FALSE;

  if (g_strcmp0 (nma_mobile_access_method_get_username (access_method),
                 nm_setting_gsm_get_username (NM_SETTING_GSM (setting))) != 0)
    return FALSE;

  if (g_strcmp0 (nma_mobile_access_method_get_password (access_method),
                 cc_wwan_data_apn_get_password (apn)) != 0)
    return FALSE;

  return TRUE;
}

static CcWwanDataApn *
wwan_data_find_matching_apn (CcWwanData            *self,
                             NMAMobileAccessMethod *access_method)
{
  CcWwanDataApn *apn;
  guint i, n_items;

  n_items = g_list_model_get_n_items (G_LIST_MODEL (self->apn_list));

  for (i = 0; i < n_items; i++)
    {
      apn = g_list_model_get_item (G_LIST_MODEL (self->apn_list), i);

      if (apn->access_method == access_method)
        return apn;

      if (wwan_data_apn_are_same (apn, access_method))
        return apn;

      g_object_unref (apn);
    }

  return NULL;
}

static gboolean
wwan_data_nma_method_is_mms (NMAMobileAccessMethod *method)
{
  const char *str;

  str = nma_mobile_access_method_get_3gpp_apn (method);
  if (str && strcasestr (str, "mms"))
    return TRUE;

  str = nma_mobile_access_method_get_name (method);
  if (str && strcasestr (str, "mms"))
    return TRUE;

  return FALSE;
}

static void
wwan_data_update_apn_list_db (CcWwanData *self)
{
  GSList *apn_methods = NULL, *l;
  g_autoptr(GError) error = NULL;
  guint i = 0;

  if (!self->sim || !self->operator_code || self->apn_list_updated)
    return;

  if (!self->apn_list)
    return;

  if (!self->apn_db)
    self->apn_db = nma_mobile_providers_database_new_sync (NULL, NULL, NULL, &error);

  if (error)
    {
      g_warning ("%s", error->message);
      return;
    }

  if (!self->apn_provider)
    self->apn_provider = nma_mobile_providers_database_lookup_3gpp_mcc_mnc (self->apn_db,
                                                                            self->operator_code);

  if (self->apn_provider)
    apn_methods = nma_mobile_provider_get_methods (self->apn_provider);

  self->apn_list_updated = TRUE;

  for (l = apn_methods; l; l = l->next, i++)
    {
      g_autoptr(CcWwanDataApn) apn = NULL;

      /* We don’t list MMS APNs */
      if (wwan_data_nma_method_is_mms (l->data))
        continue;

      apn = wwan_data_find_matching_apn (self, l->data);

      /* Prepend the item in order */
      if (!apn)
        {
          apn = cc_wwan_data_apn_new ();
          apn->access_method = l->data;
          g_list_store_insert (self->apn_list, i, apn);
        }

      apn->access_method = l->data;
    }
}

static void
wwan_data_update_apn_list (CcWwanData *self)
{
  const GPtrArray *nm_connections;
  guint i;

  if (self->apn_list || !self->sim || !self->nm_device ||
      nm_device_get_state (self->nm_device) <= NM_DEVICE_STATE_UNAVAILABLE)
    return;

  if (!self->apn_list)
    self->apn_list = g_list_store_new (CC_TYPE_WWAN_DATA_APN);

  if (self->nm_device)
    {
      nm_connections = nm_device_get_available_connections (self->nm_device);

      for (i = 0; i < nm_connections->len; i++)
        {
          g_autoptr(CcWwanDataApn) apn = NULL;

          apn = cc_wwan_data_apn_new ();
          apn->remote_connection = g_object_ref (nm_connections->pdata[i]);
          g_list_store_append (self->apn_list, apn);

          /* Load the default APN */
          if (!self->default_apn && self->sim_id)
            {
              NMSettingConnection *connection_setting;
              NMSettingIPConfig *ip_setting;
              NMSettingGsm *setting;
              NMConnection *connection;
              const gchar *sim_id;

              connection = NM_CONNECTION (apn->remote_connection);
              setting = nm_connection_get_setting_gsm (connection);
              connection_setting = nm_connection_get_setting_connection (connection);
              sim_id = nm_setting_gsm_get_sim_id (setting);

              if (sim_id && *sim_id && g_str_equal (sim_id, self->sim_id))
                {
                  self->default_apn = apn;
                  self->home_only = nm_setting_gsm_get_home_only (setting);
                  self->data_enabled = nm_setting_connection_get_autoconnect (connection_setting);

                  /* If any of the APN has a high priority, the device have high priority */
                  ip_setting = nm_connection_get_setting_ip4_config (connection);
                  if (nm_setting_ip_config_get_route_metric (ip_setting) == CC_WWAN_ROUTE_PRIORITY_HIGH)
                    self->priority = CC_WWAN_APN_PRIORITY_HIGH;
                }
            }
        }
    }
}

static void
wwan_device_state_changed_cb (CcWwanData *self)
{
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ENABLED]);
}

static void
wwan_device_3gpp_operator_code_changd_cb (CcWwanData *self)
{
  MMModem3gpp *modem_3gpp;

  modem_3gpp = mm_object_peek_modem_3gpp (self->mm_object);

  if (!self->operator_code)
    {
      self->operator_code = mm_modem_3gpp_dup_operator_code (modem_3gpp);

      if (self->operator_code)
        {
          wwan_data_update_apn_list (self);
          wwan_data_update_apn_list_db (self);
        }
    }
}

static void
cc_wwan_data_get_property (GObject    *object,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  CcWwanData *self = (CcWwanData *)object;

  switch (prop_id)
    {
    case PROP_ERROR:
      g_value_set_boolean (value, self->error != NULL);
      break;

    case PROP_ENABLED:
      g_value_set_boolean (value, cc_wwan_data_get_enabled (self));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
cc_wwan_data_dispose (GObject *object)
{
  CcWwanData *self = (CcWwanData *)object;

  g_clear_pointer (&self->sim_id, g_free);
  g_clear_pointer (&self->operator_code, g_free);
  g_clear_error (&self->error);
  g_clear_object (&self->apn_list);
  g_clear_object (&self->modem);
  g_clear_object (&self->mm_object);
  g_clear_object (&self->nm_client);
  g_clear_object (&self->active_connection);
  g_clear_object (&self->apn_db);

  G_OBJECT_CLASS (cc_wwan_data_parent_class)->dispose (object);
}

static void
cc_wwan_data_class_init (CcWwanDataClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = cc_wwan_data_get_property;
  object_class->dispose = cc_wwan_data_dispose;

  properties[PROP_ERROR] =
    g_param_spec_boolean ("error",
                          "Error",
                          "Set if some Error occurs",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  properties[PROP_ENABLED] =
    g_param_spec_boolean ("enabled",
                          "Enabled",
                          "Get if the data is enabled",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, N_PROPS, properties);
}

static void
cc_wwan_data_init (CcWwanData *self)
{
  self->home_only = TRUE;
  self->priority = CC_WWAN_APN_PRIORITY_LOW;
}

/**
 * cc_wwan_data_new:
 * @mm_object: An #MMObject
 * @nm_client: An #NMClient
 *
 * Create a new device data representing the given
 * @mm_object. If @mm_object isn’t a 3G/CDMA/LTE
 * modem, %NULL will be returned
 *
 * Returns: A #CcWwanData or %NULL.
 */
CcWwanData *
cc_wwan_data_new (MMObject *mm_object,
                  NMClient *nm_client)
{
  CcWwanData *self;
  NMDevice *nm_device = NULL;
  g_autoptr(MMModem) modem = NULL;
  NMDeviceModemCapabilities capabilities = 0;

  g_return_val_if_fail (MM_IS_OBJECT (mm_object), NULL);
  g_return_val_if_fail (NM_CLIENT (nm_client), NULL);

  modem = mm_object_get_modem (mm_object);

  if (modem)
    nm_device = nm_client_get_device_by_iface (nm_client,
                                               mm_modem_get_primary_port (modem));

  if (NM_IS_DEVICE_MODEM (nm_device))
    capabilities = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (nm_device));

  if (!(capabilities & (NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS
                        | NM_DEVICE_MODEM_CAPABILITY_LTE)))
    return NULL;

  self = g_object_new (CC_TYPE_WWAN_DATA, NULL);

  self->nm_client = g_object_ref (nm_client);
  self->mm_object = g_object_ref (mm_object);
  self->modem = g_steal_pointer (&modem);
  self->sim = mm_modem_get_sim_sync (self->modem, NULL, NULL);
  self->sim_id = mm_sim_dup_identifier (self->sim);
  self->operator_code = mm_sim_dup_operator_identifier (self->sim);
  self->nm_device = g_object_ref (nm_device);
  self->active_connection = nm_device_get_active_connection (nm_device);

  if (!self->operator_code)
    {
      MMModem3gpp *modem_3gpp;

      modem_3gpp = mm_object_peek_modem_3gpp (mm_object);
      if (modem_3gpp)
        {
          g_signal_connect_object (modem_3gpp, "notify::operator-code",
                                   G_CALLBACK (wwan_device_3gpp_operator_code_changd_cb),
                                   self, G_CONNECT_SWAPPED);
          wwan_device_3gpp_operator_code_changd_cb (self);
        }
    }

  if (self->active_connection)
    g_object_ref (self->active_connection);

  g_signal_connect_object (self->nm_device, "notify::state",
                           G_CALLBACK (wwan_device_state_changed_cb),
                           self, G_CONNECT_SWAPPED);

  wwan_data_update_apn_list (self);
  wwan_data_update_apn_list_db (self);

  return self;
}

GError *
cc_wwan_data_get_error (CcWwanData *self)
{
  g_return_val_if_fail (CC_IS_WWAN_DATA (self), NULL);

  return self->error;
}

const gchar *
cc_wwan_data_get_simple_html_error (CcWwanData *self)
{
  g_return_val_if_fail (CC_IS_WWAN_DATA (self), NULL);

  if (!self->error)
    return NULL;

  if (g_error_matches (self->error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
    return _("Operation Cancelled");

  if (g_error_matches (self->error, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED))
    return _("<b>Error:</b> Access denied changing settings");

  if (self->error->domain == MM_MOBILE_EQUIPMENT_ERROR)
    return _("<b>Error:</b> Mobile Equipment Error");

  return NULL;
}

GListModel *
cc_wwan_data_get_apn_list (CcWwanData *self)
{
  g_return_val_if_fail (CC_IS_WWAN_DATA (self), NULL);

  if (!self->apn_list)
    wwan_data_update_apn_list (self);

  return G_LIST_MODEL (self->apn_list);
}

static gboolean
wwan_data_apn_is_new (CcWwanDataApn *apn)
{
  return apn->remote_connection == NULL;
}

static void
wwan_data_update_apn (CcWwanData    *self,
                      CcWwanDataApn *apn,
                      NMConnection  *connection)
{
  NMSetting *setting;
  const gchar *name, *username, *password, *apn_name;
  gint dns_priority, route_metric;

  setting = NM_SETTING (nm_connection_get_setting_connection (connection));

  g_object_set (setting,
                NM_SETTING_CONNECTION_AUTOCONNECT, self->data_enabled,
                NULL);

  setting = NM_SETTING (nm_connection_get_setting_gsm (connection));

  g_object_set (setting,
                NM_SETTING_GSM_HOME_ONLY, self->home_only,
                NULL);

  setting = NM_SETTING (nm_connection_get_setting_ip4_config (connection));
  if (self->priority == CC_WWAN_APN_PRIORITY_HIGH &&
      self->default_apn == apn)
    {
      dns_priority = CC_WWAN_DNS_PRIORITY_HIGH;
      route_metric = CC_WWAN_ROUTE_PRIORITY_HIGH;
    }
  else
    {
      dns_priority = CC_WWAN_DNS_PRIORITY_LOW;
      route_metric = CC_WWAN_ROUTE_PRIORITY_LOW;
    }

  g_object_set (setting,
                NM_SETTING_IP_CONFIG_DNS_PRIORITY, dns_priority,
                NM_SETTING_IP_CONFIG_ROUTE_METRIC, (gint64)route_metric,
                NULL);

  if (apn->access_method && !apn->remote_connection)
    {
      name = nma_mobile_access_method_get_name (apn->access_method);
      username = nma_mobile_access_method_get_username (apn->access_method);
      password = nma_mobile_access_method_get_password (apn->access_method);
      apn_name = nma_mobile_access_method_get_3gpp_apn (apn->access_method);
    }
  else
    {
      return;
    }

  setting = NM_SETTING (nm_connection_get_setting_gsm (connection));
  g_object_set (setting,
                NM_SETTING_GSM_USERNAME, username,
                NM_SETTING_GSM_PASSWORD, password,
                NM_SETTING_GSM_APN, apn_name,
                NULL);

  setting = NM_SETTING (nm_connection_get_setting_connection (connection));

  g_object_set (setting,
                NM_SETTING_CONNECTION_ID, name,
                NULL);
}

static gint
wwan_data_get_apn_index (CcWwanData    *self,
                         CcWwanDataApn *apn)
{
  GListModel *model;
  guint i, n_items;

  model = G_LIST_MODEL (self->apn_list);
  n_items = g_list_model_get_n_items (model);

  for (i = 0; i < n_items; i++)
    {
      g_autoptr(CcWwanDataApn) cached_apn = NULL;

      cached_apn = g_list_model_get_item (model, i);

      if (apn == cached_apn)
        return i;
    }

  return -1;
}

static void
cc_wwan_data_connection_updated_cb (GObject      *object,
                                    GAsyncResult *result,
                                    gpointer      user_data)
{
  CcWwanData *self;
  CcWwanDataApn *apn;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GError) error = NULL;

  self = g_task_get_source_object (G_TASK (task));
  apn = g_task_get_task_data (G_TASK (task));

  nm_remote_connection_commit_changes_finish (apn->remote_connection,
                                              result, &error);
  if (!error)
    {
      guint apn_index;
      apn_index = wwan_data_get_apn_index (self, apn);

      if (apn_index >= 0)
        g_list_model_items_changed (G_LIST_MODEL (self->apn_list),
                                    apn_index, 1, 1);
      else
        g_warning ("APN ‘%s’ not in APN list",
                   cc_wwan_data_apn_get_name (apn));

      apn->modified = FALSE;
      g_task_return_boolean (task, TRUE);
    }
  else
    {
      g_task_return_error (task, g_steal_pointer (&error));
    }
}

static void
cc_wwan_data_new_connection_added_cb (GObject      *object,
                                      GAsyncResult *result,
                                      gpointer      user_data)
{
  CcWwanData *self;
  CcWwanDataApn *apn;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GError) error = NULL;

  self = g_task_get_source_object (G_TASK (task));
  apn = g_task_get_task_data (G_TASK (task));
  apn->remote_connection = nm_client_add_connection_finish (self->nm_client,
                                                            result, &error);
  if (!error)
    {
      apn->modified = FALSE;

      /* If APN has access method, it’s already on the list */
      if (!apn->access_method)
        {
          g_list_store_append (self->apn_list, apn);
          g_object_unref (apn);
        }

      g_task_return_pointer (task, apn, NULL);
    }
  else
    {
      g_task_return_error (task, g_steal_pointer (&error));
    }
}

void
cc_wwan_data_save_apn (CcWwanData          *self,
                       CcWwanDataApn       *apn,
                       GCancellable        *cancellable,
                       GAsyncReadyCallback  callback,
                       gpointer             user_data)
{
  NMConnection *connection = NULL;
  g_autoptr(GTask) task = NULL;

  g_return_if_fail (CC_IS_WWAN_DATA (self));
  g_return_if_fail (CC_IS_WWAN_DATA_APN (apn));
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

  task = g_task_new (self, cancellable, callback, user_data);
  g_task_set_task_data (task, apn, NULL);

  connection = wwan_data_get_nm_connection (apn);

  /* If the item has a remote connection, it should already be saved.
   * We should save it again only if it got modified */
  if (apn->remote_connection && !apn->modified)
    {
      g_task_return_pointer (task, apn, NULL);
      return;
    }

  wwan_data_update_apn (self, apn, connection);
  if (wwan_data_apn_is_new (apn))
    {
      nm_client_add_connection_async (self->nm_client, apn->nm_connection,
                                      TRUE, cancellable,
                                      cc_wwan_data_new_connection_added_cb,
                                      g_steal_pointer (&task));
    }
  else
    {
      nm_remote_connection_commit_changes_async (apn->remote_connection, TRUE,
                                                 cancellable,
                                                 cc_wwan_data_connection_updated_cb,
                                                 g_steal_pointer (&task));
    }
}

CcWwanDataApn *
cc_wwan_data_save_apn_finish (CcWwanData    *self,
                              GAsyncResult  *result,
                              GError       **error)
{
  g_return_val_if_fail (CC_IS_WWAN_DATA (self), NULL);
  g_return_val_if_fail (G_IS_TASK (result), NULL);

  return g_task_propagate_pointer (G_TASK (result), error);
}

static void
cc_wwan_data_activated_cb (GObject      *object,
                           GAsyncResult *result,
                           gpointer      user_data)
{
  CcWwanData *self;
  NMActiveConnection *connection;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GError) error = NULL;

  self = g_task_get_source_object (G_TASK (task));
  connection = nm_client_activate_connection_finish (self->nm_client,
                                                     result, &error);
  if (connection)
    {
      g_set_object (&self->active_connection, connection);
      g_task_return_boolean (task, TRUE);
    }
  else
    {
      g_task_return_error (task, g_steal_pointer (&error));
    }

  if (error)
    g_warning ("Error: %s", error->message);
}

static void
cc_wwan_data_disconnect_cb (GObject      *object,
                            GAsyncResult *result,
                            gpointer      user_data)
{
  CcWwanData *self;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GError) error = NULL;

  self = g_task_get_source_object (G_TASK (task));
  if (nm_device_disconnect_finish (self->nm_device, result, &error))
    {
      g_clear_object (&self->active_connection);
      g_task_return_boolean (task, TRUE);
    }
  else
    {
      g_task_return_error (task, g_steal_pointer (&error));
    }

  if (error)
    g_warning ("Error: %s", error->message);
}

static void
cc_wwan_data_settings_saved_cb (GObject      *object,
                                GAsyncResult *result,
                                gpointer      user_data)
{
  CcWwanData *self;
  GCancellable *cancellable;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GError) error = NULL;

  self = g_task_get_source_object (G_TASK (task));
  cancellable = g_task_get_cancellable (G_TASK (task));

  if (!cc_wwan_data_save_apn_finish (self, result, &error))
    {
      g_task_return_error (task, g_steal_pointer (&error));
      return;
    }

  self->default_apn->modified = FALSE;

  if (self->data_enabled)
    {
      nm_client_activate_connection_async (self->nm_client,
                                           NM_CONNECTION (self->default_apn->remote_connection),
                                           self->nm_device,
                                           NULL, cancellable,
                                           cc_wwan_data_activated_cb,
                                           g_steal_pointer (&task));
    }
  else
    {
      nm_device_disconnect_async (self->nm_device,
                                  cancellable,
                                  cc_wwan_data_disconnect_cb,
                                  g_steal_pointer (&task));
    }
}

/**
 * cc_wwan_data_save_settings:
 * @cancellable: (nullable): a #GCancellable or %NULL
 * @callback: a #GAsyncReadyCallback, or %NULL
 * @user_data: closure data for @callback
 *
 * Save default settings to disk and apply changes.
 * If the default APN has data enabled, the data is
 * activated after the settings are saved.
 *
 * It’s a programmer error to call this function without
 * a default APN set.
 * Finish with cc_wwan_data_save_settings_finish().
 */
void
cc_wwan_data_save_settings (CcWwanData          *self,
                            GCancellable        *cancellable,
                            GAsyncReadyCallback  callback,
                            gpointer             user_data)
{
  NMConnection *connection;
  NMSetting *setting;
  g_autoptr(GTask) task = NULL;

  g_return_if_fail (CC_IS_WWAN_DATA (self));
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
  g_return_if_fail (self->default_apn != NULL);

  task = g_task_new (self, cancellable, callback, user_data);

  /* Reset old settings to default value */
  if (self->old_default_apn && self->old_default_apn->remote_connection)
    {
      connection = NM_CONNECTION (self->old_default_apn->remote_connection);

      setting = NM_SETTING (nm_connection_get_setting_gsm (connection));
      g_object_set (G_OBJECT (setting),
                    NM_SETTING_GSM_HOME_ONLY, TRUE,
                    NM_SETTING_GSM_SIM_ID, NULL,
                    NULL);

      setting = NM_SETTING (nm_connection_get_setting_ip4_config (connection));
      g_object_set (setting,
                    NM_SETTING_IP_CONFIG_DNS_PRIORITY, CC_WWAN_DNS_PRIORITY_LOW,
                    NM_SETTING_IP_CONFIG_ROUTE_METRIC, (gint64)CC_WWAN_ROUTE_PRIORITY_LOW,
                    NULL);

      setting = NM_SETTING (nm_connection_get_setting_connection (connection));
      g_object_set (G_OBJECT (setting),
                    NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
                    NULL);

      nm_remote_connection_commit_changes (NM_REMOTE_CONNECTION (connection),
                                           TRUE, cancellable, NULL);
      self->old_default_apn->modified = FALSE;
      self->old_default_apn = NULL;
    }

  self->default_apn->modified = TRUE;
  connection = wwan_data_get_nm_connection (self->default_apn);

  setting = NM_SETTING (nm_connection_get_setting_gsm (connection));
  g_object_set (G_OBJECT (setting),
                NM_SETTING_GSM_HOME_ONLY, self->home_only,
                NM_SETTING_GSM_SIM_ID, self->sim_id,
                NULL);

  cc_wwan_data_save_apn (self, self->default_apn, cancellable,
                         cc_wwan_data_settings_saved_cb,
                         g_steal_pointer (&task));
}

gboolean
cc_wwan_data_save_settings_finish (CcWwanData    *self,
                                   GAsyncResult  *result,
                                   GError       **error)
{
  g_return_val_if_fail (CC_IS_WWAN_DATA (self), FALSE);
  g_return_val_if_fail (G_IS_TASK (result), FALSE);

  return g_task_propagate_boolean (G_TASK (result), error);
}

gboolean
cc_wwan_data_delete_apn (CcWwanData     *self,
                         CcWwanDataApn  *apn,
                         GCancellable   *cancellable,
                         GError        **error)
{
  NMRemoteConnection *connection = NULL;
  gboolean ret = FALSE;
  gint apn_index;

  g_return_val_if_fail (CC_IS_WWAN_DATA (self), FALSE);
  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
  g_return_val_if_fail (CC_IS_WWAN_DATA_APN (apn), FALSE);
  g_return_val_if_fail (error != NULL, FALSE);

  apn_index = wwan_data_get_apn_index (self, apn);
  if (apn_index == -1)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
                   "APN not found for the connection");
      return FALSE;
    }

  connection = g_steal_pointer (&apn->remote_connection);
  wwan_data_apn_reset (apn);

  if (connection)
    ret = nm_remote_connection_delete (connection, cancellable, error);

  if (!ret)
    {
      apn->remote_connection = connection;
      *error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
                            "Deleting APN from NetworkManager failed");
      return ret;
    }

  g_object_unref (connection);

  /* We remove the item only if it's not in the mobile provider database */
  if (!apn->access_method)
    {
      if (self->default_apn == apn)
        self->default_apn = NULL;

      g_list_store_remove (self->apn_list, apn_index);

      return TRUE;
    }

  *error = g_error_new (G_IO_ERROR, G_IO_ERROR_READ_ONLY,
                        "Deleting APN from NetworkManager failed");
  return FALSE;
}

CcWwanDataApn *
cc_wwan_data_get_default_apn (CcWwanData *self)
{
  g_return_val_if_fail (CC_IS_WWAN_DATA (self), NULL);

  return self->default_apn;
}

gboolean
cc_wwan_data_set_default_apn (CcWwanData    *self,
                              CcWwanDataApn *apn)
{
  NMConnection *connection;
  NMSetting *setting;

  g_return_val_if_fail (CC_IS_WWAN_DATA (self), FALSE);
  g_return_val_if_fail (CC_IS_WWAN_DATA_APN (apn), FALSE);

  if (self->default_apn == apn)
    return FALSE;

  /*
   * APNs are bound to the SIM, not the modem device.
   * This will let the APN work if the same SIM inserted
   * in a different device, and not enable data if a
   * different SIM is inserted to the modem.
   */
  apn->modified = TRUE;
  self->old_default_apn = self->default_apn;
  self->default_apn = apn;
  connection = wwan_data_get_nm_connection (apn);
  setting = NM_SETTING (nm_connection_get_setting_gsm (connection));

  if (self->sim_id)
    g_object_set (G_OBJECT (setting),
                  NM_SETTING_GSM_SIM_ID, self->sim_id, NULL);

  return TRUE;
}

gboolean
cc_wwan_data_get_enabled (CcWwanData *self)
{
  NMSettingConnection *setting;
  NMConnection *connection;
  NMDeviceState state;

  g_return_val_if_fail (CC_IS_WWAN_DATA (self), FALSE);

  state = nm_device_get_state (self->nm_device);

  if (state == NM_DEVICE_STATE_DISCONNECTED ||
      state == NM_DEVICE_STATE_DEACTIVATING)
    if (nm_device_get_state_reason (self->nm_device) == NM_DEVICE_STATE_REASON_USER_REQUESTED)
      return FALSE;

  if (nm_device_get_active_connection (self->nm_device) != NULL)
    return TRUE;

  if (!self->default_apn || !self->default_apn->remote_connection)
    return FALSE;

  connection = NM_CONNECTION (self->default_apn->remote_connection);
  setting = nm_connection_get_setting_connection (connection);

  return nm_setting_connection_get_autoconnect (setting);
}

/**
 * cc_wwan_data_set_enabled:
 * @self: A #CcWwanData
 * @enable_data: whether to enable data
 *
 * Enable data for the device.  The settings is
 * saved to disk only after a default APN is set.
 *
 * If the data is enabled, the device will automatically
 * turn data on everytime the same SIM is available.
 * The data set is bound to the SIM, not the modem device.
 *
 * Use @cc_wwan_data_save_apn() with the default APN
 * to save the changes and really enable/disable data.
 */
void
cc_wwan_data_set_enabled (CcWwanData *self,
                          gboolean    enable_data)
{
  g_return_if_fail (CC_IS_WWAN_DATA (self));

  self->data_enabled = !!enable_data;

  if (self->default_apn)
    self->default_apn->modified = TRUE;
}

gboolean
cc_wwan_data_get_roaming_enabled (CcWwanData *self)
{
  g_return_val_if_fail (CC_IS_WWAN_DATA (self), FALSE);

  if (!self->default_apn)
    return FALSE;

  return !self->home_only;
}

/**
 * cc_wwan_data_apn_set_roaming_enabled:
 * @self: A #CcWwanData
 * @enable_roaming: whether to enable roaming or not
 *
 * Enable roaming for the device.  The settings is
 * saved to disk only after a default APN is set.
 *
 * Use @cc_wwan_data_save_apn() with the default APN
 * to save the changes and really enable/disable data.
 */
void
cc_wwan_data_set_roaming_enabled (CcWwanData *self,
                                  gboolean    enable_roaming)
{
  g_return_if_fail (CC_IS_WWAN_DATA (self));

  self->home_only = !enable_roaming;

  if (self->default_apn)
    self->default_apn->modified = TRUE;
}

static void
cc_wwan_data_apn_finalize (GObject *object)
{
  CcWwanDataApn *apn = CC_WWAN_DATA_APN (object);

  wwan_data_apn_reset (apn);
  g_clear_pointer (&apn->access_method,
                   nma_mobile_access_method_unref);

  G_OBJECT_CLASS (cc_wwan_data_parent_class)->finalize (object);
}

static void
cc_wwan_data_apn_class_init (CcWwanDataApnClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = cc_wwan_data_apn_finalize;
}

static void
cc_wwan_data_apn_init (CcWwanDataApn *apn)
{
}

CcWwanDataApn *
cc_wwan_data_apn_new (void)
{
  return g_object_new (CC_TYPE_WWAN_DATA_APN, NULL);
}

/**
 * cc_wwan_data_apn_get_name:
 * @apn: A #CcWwanDataApn
 *
 * Get the Name of @apn
 *
 * Returns: (transfer none): The Name of @apn
 */
const gchar *
cc_wwan_data_apn_get_name (CcWwanDataApn *apn)
{
  g_return_val_if_fail (CC_IS_WWAN_DATA_APN (apn), "");

  if (apn->remote_connection)
    return nm_connection_get_id (NM_CONNECTION (apn->remote_connection));

  if (apn->access_method)
    return nma_mobile_access_method_get_name (apn->access_method);

  return "";
}

/**
 * cc_wwan_data_apn_set_name:
 * @apn: A #CcWwanDataApn
 * @name: The name to be given for APN, should not
 * be empty
 *
 * Set the name of @apn to be @name.
 *
 * @apn is only modified, use @cc_wwan_data_save_apn()
 * to save the changes.
 */
void
cc_wwan_data_apn_set_name (CcWwanDataApn *apn,
                           const gchar   *name)
{
  NMConnection *connection;
  NMSettingConnection *setting;

  g_return_if_fail (CC_IS_WWAN_DATA_APN (apn));
  g_return_if_fail (name != NULL);
  g_return_if_fail (*name != '\0');

  if (g_str_equal (cc_wwan_data_apn_get_name (apn), name))
    return;

  apn->modified = TRUE;
  connection = wwan_data_get_nm_connection (apn);
  setting = nm_connection_get_setting_connection (connection);
  g_object_set (G_OBJECT (setting),
                NM_SETTING_CONNECTION_ID, name,
                NULL);
}

/**
 * cc_wwan_data_apn_get_apn:
 * @apn: A #CcWwanDataApn
 *
 * Get the APN of @apn
 *
 * Returns: (transfer none): The APN of @apn
 */
const gchar *
cc_wwan_data_apn_get_apn (CcWwanDataApn *apn)
{
  const gchar *apn_name = "";

  g_return_val_if_fail (CC_IS_WWAN_DATA_APN (apn), "");

  if (apn->remote_connection)
    {
      NMSettingGsm *setting;

      setting = nm_connection_get_setting_gsm (NM_CONNECTION (apn->remote_connection));
      apn_name = nm_setting_gsm_get_apn (setting);
    }
  else if (apn->access_method)
    {
      apn_name = nma_mobile_access_method_get_3gpp_apn (apn->access_method);
    }

  return apn_name ? apn_name : "";
}

/**
 * cc_wwan_data_apn_set_apn:
 * @apn: A #CcWwanDataApn
 * @apn_name: The apn to be used, should not be
 * empty
 *
 * Set the APN of @apn to @apn_name. @apn_name is
 * usually a URL like “example.com” or a simple string
 * like “internet”
 *
 * @apn is only modified, use @cc_wwan_data_save_apn()
 * to save the changes.
 */
void
cc_wwan_data_apn_set_apn (CcWwanDataApn *apn,
                          const gchar   *apn_name)
{
  NMConnection *connection;
  NMSettingGsm *setting;

  g_return_if_fail (CC_IS_WWAN_DATA_APN (apn));
  g_return_if_fail (apn_name != NULL);
  g_return_if_fail (*apn_name != '\0');

  if (g_str_equal (cc_wwan_data_apn_get_apn (apn), apn_name))
    return;

  apn->modified = TRUE;
  connection = wwan_data_get_nm_connection (apn);
  setting = nm_connection_get_setting_gsm (connection);
  g_object_set (G_OBJECT (setting),
                NM_SETTING_GSM_APN, apn_name,
                NULL);
}

/**
 * cc_wwan_data_apn_get_username:
 * @apn: A #CcWwanDataApn
 *
 * Get the Username of @apn
 *
 * Returns: (transfer none): The Username of @apn
 */
const gchar *
cc_wwan_data_apn_get_username (CcWwanDataApn *apn)
{
  const gchar *username = "";

  g_return_val_if_fail (CC_IS_WWAN_DATA_APN (apn), "");

  if (apn->remote_connection)
    {
      NMSettingGsm *setting;

      setting = nm_connection_get_setting_gsm (NM_CONNECTION (apn->remote_connection));
      username = nm_setting_gsm_get_username (setting);
    }
  else if (apn->access_method)
    {
      username = nma_mobile_access_method_get_username (apn->access_method);
    }

  return username ? username : "";
}

/**
 * cc_wwan_data_apn_set_username:
 * @apn: A #CcWwanDataAPN
 * @username: The username to be used
 *
 * Set the Username of @apn to @username.
 *
 * @apn is only modified, use @cc_wwan_data_save_apn()
 * to save the changes.
 */
void
cc_wwan_data_apn_set_username (CcWwanDataApn *apn,
                               const gchar   *username)
{
  NMConnection *connection;
  NMSettingGsm *setting;

  g_return_if_fail (CC_IS_WWAN_DATA_APN (apn));

  if (username && !*username)
    username = NULL;

  if (g_strcmp0 (cc_wwan_data_apn_get_username (apn), username) == 0)
    return;

  apn->modified = TRUE;
  connection = wwan_data_get_nm_connection (apn);
  setting = nm_connection_get_setting_gsm (connection);
  g_object_set (G_OBJECT (setting),
                NM_SETTING_GSM_USERNAME, username,
                NULL);
}

/**
 * cc_wwan_data_apn_get_password:
 * @apn: A #CcWwanDataApn
 *
 * Get the Password of @apn
 *
 * Returns: (transfer none): The Password of @apn
 */
const gchar *
cc_wwan_data_apn_get_password (CcWwanDataApn *apn)
{
  const gchar *password = "";

  g_return_val_if_fail (CC_IS_WWAN_DATA_APN (apn), "");

  if (NM_IS_REMOTE_CONNECTION (apn->remote_connection))
    {
      g_autoptr(GVariant) secrets = NULL;
      g_autoptr(GError) error = NULL;

      secrets = nm_remote_connection_get_secrets (NM_REMOTE_CONNECTION (apn->remote_connection),
                                                  "gsm", NULL, &error);

      if (!error)
        nm_connection_update_secrets (NM_CONNECTION (apn->remote_connection),
                                      "gsm", secrets, &error);

      if (error)
        {
          g_warning ("Error: %s", error->message);
          return "";
        }
    }

  if (apn->remote_connection)
    {
      NMSettingGsm *setting;

      setting = nm_connection_get_setting_gsm (NM_CONNECTION (apn->remote_connection));
      password = nm_setting_gsm_get_password (setting);
    }
  else if (apn->access_method)
    {
      password = nma_mobile_access_method_get_password (apn->access_method);
    }

  return password ? password : "";
}

/**
 * cc_wwan_data_apn_set_password:
 * @apn: A #CcWwanDataApn
 * @password: The password to be used
 *
 * Set the Password of @apn to @password.
 *
 * @apn is only modified, use @cc_wwan_data_save_apn()
 * to save the changes.
 */
void
cc_wwan_data_apn_set_password (CcWwanDataApn *apn,
                               const gchar   *password)
{
  NMConnection *connection;
  NMSettingGsm *setting;

  g_return_if_fail (CC_IS_WWAN_DATA_APN (apn));

  if (password && !*password)
    password = NULL;

  if (g_strcmp0 (cc_wwan_data_apn_get_password (apn), password) == 0)
    return;

  apn->modified = TRUE;
  connection = wwan_data_get_nm_connection (apn);
  setting = nm_connection_get_setting_gsm (connection);
  g_object_set (G_OBJECT (setting),
                NM_SETTING_GSM_PASSWORD, password,
                NULL);
}

gint
cc_wwan_data_get_priority (CcWwanData *self)
{
  CcWwanDataApn *apn;
  NMSettingIPConfig *setting;

  g_return_val_if_fail (CC_IS_WWAN_DATA (self),
                        CC_WWAN_APN_PRIORITY_LOW);

  apn = self->default_apn;

  if (!apn || !apn->remote_connection)
    return CC_WWAN_APN_PRIORITY_LOW;

  setting = nm_connection_get_setting_ip4_config (NM_CONNECTION (apn->remote_connection));

  /* Lower the number, higher the priority */
  if (nm_setting_ip_config_get_route_metric (setting) <= CC_WWAN_ROUTE_PRIORITY_HIGH)
    return CC_WWAN_APN_PRIORITY_HIGH;
  else
    return CC_WWAN_APN_PRIORITY_LOW;
}

void
cc_wwan_data_set_priority (CcWwanData *self,
                           int         priority)
{
  g_return_if_fail (CC_IS_WWAN_DATA (self));
  g_return_if_fail (priority == CC_WWAN_APN_PRIORITY_LOW ||
                    priority == CC_WWAN_APN_PRIORITY_HIGH);

  if (self->priority == priority)
    return;

  self->priority = priority;

  if (self->default_apn)
    self->default_apn->modified = TRUE;
}