Module: Yast::SnapperDialogsInclude

Defined in:
../../src/include/snapper/dialogs.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) cleanup_items(current)

generate list of items for Cleanup combo box



89
90
91
92
93
# File '../../src/include/snapper/dialogs.rb', line 89

def cleanup_items(current)
  Builtins.maplist(["timeline", "number", ""]) do |cleanup|
    Item(Id(cleanup), cleanup, cleanup == current)
  end
end

- (Object) CreateSnapshotPopup(pre_snapshots)

Popup for creating new snapshot

Returns:

  • true if new snapshot was created



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
# File '../../src/include/snapper/dialogs.rb', line 234

def CreateSnapshotPopup(pre_snapshots)
  pre_snapshots = deep_copy(pre_snapshots)
  created = false
  pre_items = Builtins.maplist(pre_snapshots) do |s|
    Item(Id(s), Builtins.tostring(s))
  end

  UI.OpenDialog(
    Opt(:decorated),
    HBox(
      HSpacing(1),
      VBox(
        VSpacing(0.5),
        HSpacing(65),
        # popup label
        Label(_("Create New Snapshot")),
        # text entry label
        TextEntry(Id("description"), _("Description"), ""),
        RadioButtonGroup(
          Id(:rb_type),
          Left(
            HVSquash(
              VBox(
                Left(
                  RadioButton(
                    Id("single"),
                    Opt(:notify),
                    # radio button label
                    _("Single snapshot"),
                    true
                  )
                ),
                Left(
                  RadioButton(
                    Id("pre"),
                    Opt(:notify),
                    # radio button label
                    _("Pre"),
                    false
                  )
                ),
                VBox(
                  Left(
                    RadioButton(
                      Id("post"),
                      Opt(:notify),
                      # radio button label, snapshot selection will follow
                      _("Post, paired with:"),
                      false
                    )
                  ),
                  HBox(
                    HSpacing(2),
                    Left(
                      ComboBox(Id(:pre_list), Opt(:notify), "", pre_items)
                    )
                  )
                )
              )
            )
          )
        ),
        # text entry label
        TextEntry(Id("userdata"), _("User data"), ""),
        # text entry label
        ComboBox(
          Id("cleanup"),
          Opt(:editable, :hstretch),
          _("Cleanup algorithm"),
          cleanup_items("")
        ),
        VSpacing(0.5),
        ButtonBox(
          PushButton(Id(:ok), Label.OKButton),
          PushButton(Id(:cancel), Label.CancelButton)
        ),
        VSpacing(0.5)
      ),
      HSpacing(1)
    )
  )

  UI.ChangeWidget(
    Id("post"),
    :Enabled,
    Ops.greater_than(Builtins.size(pre_items), 0)
  )
  UI.ChangeWidget(
    Id(:pre_list),
    :Enabled,
    Ops.greater_than(Builtins.size(pre_items), 0)
  )

  ret = nil
  args = {}
  while true
    ret = UI.UserInput
    args = {
      "type"        => UI.QueryWidget(Id(:rb_type), :Value),
      "description" => UI.QueryWidget(Id("description"), :Value),
      "pre"         => UI.QueryWidget(Id(:pre_list), :Value),
      "cleanup"     => UI.QueryWidget(Id("cleanup"), :Value),
      "userdata"    => get_userdata("userdata")
    }
    break if ret == :ok || ret == :cancel
  end
  UI.CloseDialog
  created = Snapper.CreateSnapshot(args) if ret == :ok
  created
end

- (Object) DeleteSnapshotPopup(snapshot)

Popup for deleting existing snapshot

Returns:

  • true if snapshot was deleted



347
348
349
350
351
352
353
354
355
356
357
358
359
# File '../../src/include/snapper/dialogs.rb', line 347

def DeleteSnapshotPopup(snapshot)
  snapshot = deep_copy(snapshot)
  # yes/no popup question
  if Popup.YesNo(
      Builtins.sformat(
        _("Really delete snapshot '%1'?"),
        Ops.get_integer(snapshot, "num", 0)
      )
    )
    return Snapper.DeleteSnapshot(snapshot)
  end
  false
end

- (Object) get_userdata(id)

transform userdata from widget to map



76
77
78
79
80
81
82
83
84
85
86
# File '../../src/include/snapper/dialogs.rb', line 76

def get_userdata(id)
  u = {}
  user_s = Convert.to_string(UI.QueryWidget(Id(id), :Value))
  Builtins.foreach(Builtins.splitstring(user_s, ",")) do |line|
    split = Builtins.splitstring(line, "=")
    if Ops.greater_than(Builtins.size(split), 1)
      Ops.set(u, Ops.get(split, 0, ""), Ops.get(split, 1, ""))
    end
  end
  deep_copy(u)
end

- (Object) initialize_snapper_dialogs(include_target)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File '../../src/include/snapper/dialogs.rb', line 30

def initialize_snapper_dialogs(include_target)
  Yast.import "UI"

  textdomain "snapper"

  Yast.import "Confirm"
  Yast.import "FileUtils"
  Yast.import "Label"
  Yast.import "Popup"
  Yast.import "Wizard"
  Yast.import "Snapper"
  Yast.import "String"

  Yast.include include_target, "snapper/helps.rb"
end

- (Object) ModifySnapshotPopup(snapshot)

Popup for modification of existing snapshot

Returns:

  • true if new snapshot was created



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
# File '../../src/include/snapper/dialogs.rb', line 108

def ModifySnapshotPopup(snapshot)
  snapshot = deep_copy(snapshot)
  modified = false
  num = Ops.get_integer(snapshot, "num", 0)
  previous_num = Ops.get_integer(snapshot, "pre_num", num)
  type = Ops.get_symbol(snapshot, "type", :none)

  pre_index = Ops.get(Snapper.id2index, previous_num, 0)
  pre_snapshot = Ops.get(Snapper.snapshots, pre_index, {})

  snapshot_term = lambda do |prefix, data|
    data = deep_copy(data)
    HBox(
      HSpacing(),
      Frame(
        "",
        HBox(
          HSpacing(0.4),
          VBox(
            # text entry label
            TextEntry(
              Id(Ops.add(prefix, "description")),
              _("Description"),
              Ops.get_string(data, "description", "")
            ),
            # text entry label
            TextEntry(
              Id(Ops.add(prefix, "userdata")),
              _("User data"),
              userdata2string(Ops.get_map(data, "userdata", {}))
            ),
            Left(
              ComboBox(
                Id(Ops.add(prefix, "cleanup")),
                Opt(:editable, :hstretch),
                # combo box label
                _("Cleanup algorithm"),
                cleanup_items(Ops.get_string(data, "cleanup", ""))
              )
            )
          ),
          HSpacing(0.4)
        )
      ),
      HSpacing()
    )
  end

  cont = VBox(
    # popup label, %1 is number
    Label(Builtins.sformat(_("Modify Snapshot %1"), num)),
    snapshot_term.call("", snapshot)
  )

  if type == :POST
    cont = VBox(
      # popup label, %1, %2 are numbers (range)
      Label(
        Builtins.sformat(_("Modify Snapshots %1 - %2"), previous_num, num)
      ),
      # label
      Left(Label(Builtins.sformat(_("Pre (%1)"), previous_num))),
      snapshot_term.call("pre_", pre_snapshot),
      VSpacing(),
      # label
      Left(Label(Builtins.sformat(_("Post (%1)"), num))),
      snapshot_term.call("", snapshot)
    )
  end

  UI.OpenDialog(
    Opt(:decorated),
    HBox(
      HSpacing(1),
      VBox(
        VSpacing(0.5),
        HSpacing(65),
        cont,
        VSpacing(0.5),
        ButtonBox(
          PushButton(Id(:ok), Label.OKButton),
          PushButton(Id(:cancel), Label.CancelButton)
        ),
        VSpacing(0.5)
      ),
      HSpacing(1)
    )
  )

  ret = nil
  args = {}
  pre_args = {}

  while true
    ret = UI.UserInput
    args = {
      "num"         => num,
      "description" => UI.QueryWidget(Id("description"), :Value),
      "cleanup"     => UI.QueryWidget(Id("cleanup"), :Value),
      "userdata"    => get_userdata("userdata")
    }
    if type == :POST
      pre_args = {
        "num"         => previous_num,
        "description" => UI.QueryWidget(Id("pre_description"), :Value),
        "cleanup"     => UI.QueryWidget(Id("pre_cleanup"), :Value),
        "userdata"    => get_userdata("pre_userdata")
      }
    end
    break if ret == :ok || ret == :cancel
  end
  UI.CloseDialog
  if ret == :ok
    if snapshot_modified(snapshot, args)
      modified = Snapper.ModifySnapshot(args)
    end
    if type == :POST && snapshot_modified(pre_snapshot, pre_args)
      modified = Snapper.ModifySnapshot(pre_args) || modified
    end
  end

  modified
end

- (Object) ReadDialog

Read settings dialog

Returns:

  • abort if aborted andnext otherwise



58
59
60
61
62
63
64
# File '../../src/include/snapper/dialogs.rb', line 58

def ReadDialog
  return :abort if !Confirm.MustBeRoot

  Wizard.RestoreHelp(Ops.get_string(@HELPS, "read", ""))
  ret = Snapper.Read
  ret ? :next : :abort
end

- (Object) ReallyAbort



52
53
54
# File '../../src/include/snapper/dialogs.rb', line 52

def ReallyAbort
  Popup.ReallyAbort(true)
end

- (Object) ShowDialog

Returns dialog result

Returns:

  • dialog result



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
# File '../../src/include/snapper/dialogs.rb', line 586

def ShowDialog
  # dialog caption
  caption = _("Selected Snapshot Overview")

  display_info = UI.GetDisplayInfo
  textmode = Ops.get_boolean(display_info, "TextMode", false)
  previous_file = ""
  current_file = ""

  # map of already read files
  files = {}
  # currently read subtree
  subtree = []
  tree_items = []
  open_items = {}

  snapshot = deep_copy(Snapper.selected_snapshot)
  snapshot_num = Ops.get_integer(snapshot, "num", 0)
  # map of whole tree (recursive)
  tree_map = Ops.get_map(snapshot, "tree_map", {})
  previous_num = Ops.get_integer(snapshot, "pre_num", snapshot_num)
  pre_index = Ops.get(Snapper.id2index, previous_num, 0)
  description = Ops.get_string(
    Snapper.snapshots,
    [pre_index, "description"],
    ""
  )
  pre_date = timestring(Ops.get_integer(Snapper.snapshots, [pre_index, "date"], 0))
  date = timestring(Ops.get_integer(snapshot, "date", 0))
  type = Ops.get_symbol(snapshot, "type", :NONE)
  combo_items = []
  Builtins.foreach(Snapper.snapshots) do |s|
    id = Ops.get_integer(s, "num", 0)
    if id != snapshot_num
      # '%1: %2' means 'ID: description', adapt the order if necessary
      combo_items = Builtins.add(
        combo_items,
        Item(
          Id(id),
          Builtins.sformat(
            _("%1: %2"),
            id,
            Ops.get_string(s, "description", "")
          )
        )
      )
    end
  end

  from = snapshot_num
  to = 0 # current system
  if Ops.get_symbol(snapshot, "type", :NONE) == :POST
    from = Ops.get_integer(snapshot, "pre_num", 0)
    to = snapshot_num
  elsif Ops.get_symbol(snapshot, "type", :NONE) == :PRE
    to = Ops.get_integer(snapshot, "post_num", 0)
  end

  # busy popup message
  Popup.ShowFeedback("", _("Calculating changed files..."))

  if !Builtins.haskey(snapshot, "tree_map")
    Ops.set(snapshot, "tree_map", Snapper.ReadModifiedFilesMap(from, to))
    tree_map = Ops.get_map(snapshot, "tree_map", {})
  end
  # full paths of files marked as modified, mapping to changes string
  files_index = {}
  if !Builtins.haskey(snapshot, "files_index")
    Ops.set(
      snapshot,
      "files_index",
      Snapper.ReadModifiedFilesIndex(from, to)
    )
    Ops.set(Snapper.snapshots, Snapper.selected_snapshot_index, snapshot)
  end
  Popup.ClearFeedback
  files_index = Ops.get_map(snapshot, "files_index", {})

  # update the global snapshots list
  Ops.set(Snapper.snapshots, Snapper.selected_snapshot_index, snapshot)

  snapshot_name = Builtins.tostring(snapshot_num)

  # map of all items in tree (just one level)
  selected_items = {}

  file_was_created = lambda do |file|
    Builtins.substring(
      Ops.get_string(files_index, [file, "status"], ""),
      0,
      1
    ) == "+"
  end
  file_was_removed = lambda do |file|
    Builtins.substring(
      Ops.get_string(files_index, [file, "status"], ""),
      0,
      1
    ) == "-"
  end
  # go through the map defining filesystem tree and create the widget items
  generate_tree_items = lambda do |current_path, current_branch|
    current_branch = deep_copy(current_branch)
    ret2 = []
    Builtins.foreach(current_branch) do |node, branch|
      new_path = Ops.add(Ops.add(current_path, "/"), node)
      if Builtins.haskey(files_index, new_path)
        icon_f = "16x16/apps/gdu-smart-unknown.png"
        if file_was_created.call(new_path)
          icon_f = "16x16/apps/gdu-smart-healthy.png"
        elsif file_was_removed.call(new_path)
          icon_f = "16x16/apps/gdu-smart-failing.png"
        end
        ret2 = Builtins.add(
          ret2,
          Item(
            Id(new_path),
            term(:icon, icon_f),
            node,
            false,
            generate_tree_items.call(
              new_path,
              Convert.convert(
                branch,
                :from => "map",
                :to   => "map <string, map>"
              )
            )
          )
        )
      else
        ret2 = Builtins.add(
          ret2,
          Item(
            Id(new_path),
            node,
            false,
            generate_tree_items.call(
              new_path,
              Convert.convert(
                branch,
                :from => "map",
                :to   => "map <string, map>"
              )
            )
          )
        )
      end
    end
    deep_copy(ret2)
  end

  # helper function: show the specific modification between snapshots
  show_file_modification = lambda do |file, from2, to2|
    content = VBox()
    # busy popup message
    Popup.ShowFeedback("", _("Calculating file modifications..."))
    modification = Snapper.GetFileModification(file, from2, to2)
    Popup.ClearFeedback
    status = Ops.get_list(modification, "status", [])
    if Builtins.contains(status, "created")
      # label
      content = Builtins.add(
        content,
        Left(Label(_("New file was created.")))
      )
    elsif Builtins.contains(status, "removed")
      # label
      content = Builtins.add(content, Left(Label(_("File was removed."))))
    elsif Builtins.contains(status, "no_change")
      # label
      content = Builtins.add(
        content,
        Left(Label(_("File content was not changed.")))
      )
    elsif Builtins.contains(status, "none")
      # label
      content = Builtins.add(
        content,
        Left(Label(_("File does not exist in either snapshot.")))
      )
    elsif Builtins.contains(status, "diff")
      # label
      content = Builtins.add(
        content,
        Left(Label(_("File content was modified.")))
      )
    end
    if Builtins.contains(status, "mode")
      content = Builtins.add(
        content,
        Left(
          Label(
            # text label, %1, %2 are file modes (like '-rw-r--r--')
            Builtins.sformat(
              _("File mode was changed from '%1' to '%2'."),
              Ops.get_string(modification, "mode1", ""),
              Ops.get_string(modification, "mode2", "")
            )
          )
        )
      )
    end
    if Builtins.contains(status, "user")
      content = Builtins.add(
        content,
        Left(
          Label(
            # text label, %1, %2 are user names
            Builtins.sformat(
              _("File user ownership was changed from '%1' to '%2'."),
              Ops.get_string(modification, "user1", ""),
              Ops.get_string(modification, "user2", "")
            )
          )
        )
      )
    end
    if Builtins.contains(status, "group")
      # label
      content = Builtins.add(
        content,
        Left(
          Label(
            # text label, %1, %2 are group names
            Builtins.sformat(
              _("File group ownership was changed from '%1' to '%2'."),
              Ops.get_string(modification, "group1", ""),
              Ops.get_string(modification, "group2", "")
            )
          )
        )
      )
    end

    if Builtins.haskey(modification, "diff")
      diff = String.EscapeTags(Ops.get_string(modification, "diff", ""))
      l = Builtins.splitstring(diff, "\n")
      if !textmode
        # colorize diff output
        l = Builtins.maplist(l) do |line|
          first = Builtins.substring(line, 0, 1)
          if first == "+"
            line = Builtins.sformat("<font color=blue>%1</font>", line)
          elsif first == "-"
            line = Builtins.sformat("<font color=red>%1</font>", line)
          end
          line
        end
      end
      diff = Builtins.mergestring(l, "<br>")
      if !textmode
        # show fixed font in diff
        diff = Ops.add(Ops.add("<pre>", diff), "</pre>")
      end
      content = Builtins.add(content, RichText(Id(:diff), diff))
    else
      content = Builtins.add(content, VStretch())
    end

    # button label
    restore_label = _("R&estore from First")
    # button label
    restore_label_single = _("Restore")

    if file_was_created.call(file)
      restore_label = Label.RemoveButton
      restore_label_single = Label.RemoveButton
    end

    UI.ReplaceWidget(
      Id(:diff_content),
      HBox(
        HSpacing(0.5),
        VBox(
          content,
          VSquash(
            HBox(
              HStretch(),
              type == :SINGLE ?
                Empty() :
                PushButton(Id(:restore_pre), restore_label),
              PushButton(
                Id(:restore),
                type == :SINGLE ?
                  restore_label_single :
                  _("Res&tore from Second")
              )
            )
          )
        ),
        HSpacing(0.5)
      )
    )
    if type != :SINGLE && file_was_removed.call(file)
      # file removed in 2nd snapshot cannot be restored from that snapshot
      UI.ChangeWidget(Id(:restore), :Enabled, false)
    end

    nil
  end


  # create the term for selected file
  set_entry_term = lambda do
    if current_file != "" && Builtins.haskey(files_index, current_file)
      if type == :SINGLE
        UI.ReplaceWidget(
          Id(:diff_chooser),
          HBox(
            HSpacing(0.5),
            VBox(
              VSpacing(0.2),
              RadioButtonGroup(
                Id(:rd),
                Left(
                  HVSquash(
                    VBox(
                      Left(
                        RadioButton(
                          Id(:diff_snapshot),
                          Opt(:notify),
                          # radio button label
                          _(
                            "Show the difference between snapshot and current system"
                          ),
                          true
                        )
                      ),
                      VBox(
                        Left(
                          RadioButton(
                            Id(:diff_arbitrary),
                            Opt(:notify),
                            # radio button label, snapshot selection will follow
                            _(
                              "Show the difference between current and selected snapshot:"
                            ),
                            false
                          )
                        ),
                        HBox(
                          HSpacing(2),
                          # FIXME without label, there's no shortcut!
                          Left(
                            ComboBox(
                              Id(:selection_snapshots),
                              Opt(:notify),
                              "",
                              combo_items
                            )
                          )
                        )
                      )
                    )
                  )
                )
              ),
              VSpacing()
            ),
            HSpacing(0.5)
          )
        )
        show_file_modification.call(current_file, snapshot_num, 0)
        UI.ChangeWidget(Id(:selection_snapshots), :Enabled, false)
      else
        UI.ReplaceWidget(
          Id(:diff_chooser),
          HBox(
            HSpacing(0.5),
            VBox(
              VSpacing(0.2),
              RadioButtonGroup(
                Id(:rd),
                Left(
                  HVSquash(
                    VBox(
                      Left(
                        RadioButton(
                          Id(:diff_snapshot),
                          Opt(:notify),
                          # radio button label
                          _(
                            "Show the difference between first and second snapshot"
                          ),
                          true
                        )
                      ),
                      Left(
                        RadioButton(
                          Id(:diff_pre_current),
                          Opt(:notify),
                          # radio button label
                          _(
                            "Show the difference between first snapshot and current system"
                          ),
                          false
                        )
                      ),
                      Left(
                        RadioButton(
                          Id(:diff_post_current),
                          Opt(:notify),
                          # radio button label
                          _(
                            "Show the difference between second snapshot and current system"
                          ),
                          false
                        )
                      )
                    )
                  )
                )
              ),
              VSpacing()
            ),
            HSpacing(0.5)
          )
        )
        show_file_modification.call(
          current_file,
          previous_num,
          snapshot_num
        )
      end
    else
      UI.ReplaceWidget(Id(:diff_chooser), VBox(VStretch()))
      UI.ReplaceWidget(Id(:diff_content), HBox(HStretch()))
    end

    nil
  end

  tree_label = Builtins.sformat("%1 - %2", previous_num, snapshot_num)
  # find out the path to current subvolume
  subtree_path = Snapper.GetSnapshotPath(snapshot_num)
  subtree_path = Builtins.substring(
    subtree_path,
    0,
    Builtins.find(subtree_path, ".snapshots/")
  )

  date_widget = VBox(
    HBox(
      # label, date string will follow at the end of line
      Label(Id(:pre_date), _("Time of taking the first snapshot:")),
      Right(Label(pre_date))
    ),
    HBox(
      # label, date string will follow at the end of line
      Label(Id(:post_date), _("Time of taking the second snapshot:")),
      Right(Label(date))
    )
  )
  if type == :SINGLE
    tree_label = Builtins.tostring(snapshot_num)
    date_widget = HBox(
      # label, date string will follow at the end of line
      Label(Id(:date), _("Time of taking the snapshot:")),
      Right(Label(date))
    )
  end

  contents = HBox(
    HWeight(
      1,
      VBox(
        HBox(
          HSpacing(),
          ReplacePoint(
            Id(:reptree),
            VBox(Left(Label(subtree_path)), Tree(Id(:tree), tree_label, []))
          ),
          HSpacing()
        ),
        HBox(
          HSpacing(1.5),
          HStretch(),
          textmode ?
            # button label
            PushButton(Id(:open), Opt(:key_F6), _("&Open")) :
            Empty(),
          HSpacing(1.5)
        )
      )
    ),
    HWeight(
      2,
      VBox(
        Left(Label(Id(:desc), description)),
        VSquash(VWeight(1, VSquash(date_widget))),
        VWeight(
          2,
          Frame(
            "",
            HBox(
              HSpacing(0.5),
              VBox(
                VSpacing(0.5),
                VWeight(
                  1,
                  ReplacePoint(Id(:diff_chooser), VBox(VStretch()))
                ),
                VWeight(
                  4,
                  ReplacePoint(Id(:diff_content), HBox(HStretch()))
                ),
                VSpacing(0.5)
              ),
              HSpacing(0.5)
            )
          )
        )
      )
    )
  )

  # show the dialog contents with empty tree, compute items later
  Wizard.SetContentsButtons(
    caption,
    contents,
    type == :SINGLE ?
      Ops.get_string(@HELPS, "show_single", "") :
      Ops.get_string(@HELPS, "show_couple", ""),
    # button label
    Label.CancelButton,
    _("Restore Selected")
  )

  tree_items = generate_tree_items.call("", tree_map)

  if Ops.greater_than(Builtins.size(tree_items), 0)
    UI.ReplaceWidget(
      Id(:reptree),
      VBox(
        Left(Label(subtree_path)),
        Tree(
          Id(:tree),
          Opt(:notify, :immediate, :multiSelection, :recursiveSelection),
          tree_label,
          tree_items
        )
      )
    )
    # no item is selected
    UI.ChangeWidget(:tree, :CurrentItem, nil)
  end

  current_file = ""

  set_entry_term.call

  UI.SetFocus(Id(:tree)) if textmode

  ret = nil
  while true
    event = UI.WaitForEvent
    ret = Ops.get_symbol(event, "ID")

    previous_file = current_file
    current_file = Convert.to_string(
      UI.QueryWidget(Id(:tree), :CurrentItem)
    )
    current_file = "" if current_file == nil

    # other tree events
    if ret == :tree
      # seems like tree widget emits 2 SelectionChanged events
      if current_file != previous_file
        set_entry_term.call
        UI.SetFocus(Id(:tree)) if textmode
      end
    elsif ret == :diff_snapshot
      if type == :SINGLE
        UI.ChangeWidget(Id(:selection_snapshots), :Enabled, false)
        show_file_modification.call(current_file, snapshot_num, 0)
      else
        show_file_modification.call(
          current_file,
          previous_num,
          snapshot_num
        )
      end
    elsif ret == :diff_arbitrary || ret == :selection_snapshots
      UI.ChangeWidget(Id(:selection_snapshots), :Enabled, true)
      selected_num = Convert.to_integer(
        UI.QueryWidget(Id(:selection_snapshots), :Value)
      )
      show_file_modification.call(current_file, previous_num, selected_num)
    elsif ret == :diff_pre_current
      show_file_modification.call(current_file, previous_num, 0)
    elsif ret == :diff_post_current
      show_file_modification.call(current_file, snapshot_num, 0)
    elsif ret == :abort || ret == :cancel || ret == :back
      break
    elsif (ret == :restore_pre || ret == :restore && type == :SINGLE) &&
        file_was_created.call(current_file)
      # yes/no question, %1 is file name, %2 is number
      if Popup.YesNo(
          Builtins.sformat(
            _(
              "Do you want to delete the file\n" +
                "\n" +
                "%1\n" +
                "\n" +
                "from current system?"
            ),
            Snapper.GetFileFullPath(current_file)
          )
        )
        Snapper.RestoreFiles(
          ret == :restore_pre ? previous_num : snapshot_num,
          [current_file]
        )
      end
      next
    elsif ret == :restore_pre
      # yes/no question, %1 is file name, %2 is number
      if Popup.YesNo(
          Builtins.sformat(
            _(
              "Do you want to copy the file\n" +
                "\n" +
                "%1\n" +
                "\n" +
                "from snapshot '%2' to current system?"
            ),
            Snapper.GetFileFullPath(current_file),
            previous_num
          )
        )
        Snapper.RestoreFiles(previous_num, [current_file])
      end
      next
    elsif ret == :restore
      # yes/no question, %1 is file name, %2 is number
      if Popup.YesNo(
          Builtins.sformat(
            _(
              "Do you want to copy the file\n" +
                "\n" +
                "%1\n" +
                "\n" +
                "from snapshot '%2' to current system?"
            ),
            Snapper.GetFileFullPath(current_file),
            snapshot_num
          )
        )
        Snapper.RestoreFiles(snapshot_num, [current_file])
      end
      next
    elsif ret == :next
      files2 = Convert.convert(
        UI.QueryWidget(Id(:tree), :SelectedItems),
        :from => "any",
        :to   => "list <string>"
      )
      to_restore = []
      files2 = Builtins.filter(files2) do |file|
        if Builtins.haskey(files_index, file)
          to_restore = Builtins.add(
            to_restore,
            String.EscapeTags(Snapper.GetFileFullPath(file))
          )
          next true
        else
          next false
        end
      end

      if to_restore == []
        # popup message
        Popup.Message(_("No file was selected for restoring"))
        next
      end
      # popup headline
      if Popup.AnyQuestionRichText(
          _("Restoring files"),
          # popup message, %1 is snapshot number, %2 list of files
          Builtins.sformat(
            _(
              "<p>These files will be restored from snapshot '%1':</p>\n" +
                "<p>\n" +
                "%2\n" +
                "</p>\n" +
                "<p>Files existing in original snapshot will be copied to current system.</p>\n" +
                "<p>Files that did not exist in the snapshot will be deleted.</p>Are you sure?"
            ),
            previous_num,
            Builtins.mergestring(to_restore, "<br>")
          ),
          60,
          20,
          Label.YesButton,
          Label.NoButton,
          :focus_no
        )
        Snapper.RestoreFiles(previous_num, files2)
        break
      end
      next
    else
      Builtins.y2error("unexpected retcode: %1", ret)
      next
    end
  end

  deep_copy(ret)
end

- (Object) snapshot_modified(orig, new)

compare editable parts of snapshot maps



96
97
98
99
100
101
102
103
104
# File '../../src/include/snapper/dialogs.rb', line 96

def snapshot_modified(orig, new)
  orig = deep_copy(orig)
  new = deep_copy(new)
  ret = false
  Builtins.foreach(
    Convert.convert(new, :from => "map", :to => "map <string, any>")
  ) { |key, value| ret = ret || Ops.get(orig, key) != value }
  ret
end

- (Object) SummaryDialog

Summary dialog

Returns:

  • dialog result



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
# File '../../src/include/snapper/dialogs.rb', line 363

def SummaryDialog
  # summary dialog caption
  caption = _("Snapshots")

  snapshots = deep_copy(Snapper.snapshots)
  configs = deep_copy(Snapper.configs)

  snapshot_items = []
  # lonely pre snapshots
  pre_snapshots = []

  # generate list of snapshot table items
  get_snapshot_items = lambda do
    i = -1
    snapshot_items = []
    pre_snapshots = []

    Builtins.foreach(snapshots) do |s|
      i = Ops.add(i, 1)
      num = Ops.get_integer(s, "num", 0)
      date = ""
      if num != 0
        date = timestring(Ops.get_integer(s, ["date"], 0))
      end
      userdata = userdata2string(Ops.get_map(s, "userdata", {}))
      if Ops.get_symbol(s, "type", :none) == :SINGLE
        snapshot_items = Builtins.add(
          snapshot_items,
          Item(
            Id(i),
            num,
            _("Single"),
            date,
            "",
            Ops.get_string(s, "description", ""),
            userdata
          )
        )
      elsif Ops.get_symbol(s, "type", :none) == :POST
        pre = Ops.get_integer(s, "pre_num", 0) # pre canot be 0
        index = Ops.get(Snapper.id2index, pre, -1)
        if pre == 0 || index == -1
          Builtins.y2warning(
            "something wrong - pre:%1, index:%2",
            pre,
            index
          )
          next
        end
        desc = Ops.get_string(Snapper.snapshots, [index, "description"], "")
        pre_date = timestring(Ops.get_integer(Snapper.snapshots, [index, "date"], 0))
        snapshot_items = Builtins.add(
          snapshot_items,
          Item(
            Id(i),
            Builtins.sformat("%1 - %2", pre, num),
            _("Pre & Post"),
            pre_date,
            date,
            desc,
            userdata
          )
        )
      else
        post = Ops.get_integer(s, "post_num", 0) # 0 means there's no post
        if post == 0
          Builtins.y2milestone("pre snappshot %1 does not have post", num)
          snapshot_items = Builtins.add(
            snapshot_items,
            Item(
              Id(i),
              num,
              _("Pre"),
              date,
              "",
              Ops.get_string(s, "description", ""),
              userdata
            )
          )
          pre_snapshots = Builtins.add(pre_snapshots, num)
        else
          Builtins.y2milestone("skipping pre snapshot: %1", num)
        end
      end
    end
    deep_copy(snapshot_items)
  end

  # update list of snapshots
  update_snapshots = lambda do
    # busy popup message
    Popup.ShowFeedback("", _("Reading list of snapshots..."))

    Snapper.InitializeSnapper(Snapper.current_config)
    Snapper.ReadSnapshots
    snapshots = deep_copy(Snapper.snapshots)
    Popup.ClearFeedback

    UI.ChangeWidget(Id(:snapshots_table), :Items, get_snapshot_items.call)
    UI.ChangeWidget(
      Id(:show),
      :Enabled,
      Ops.greater_than(Builtins.size(snapshot_items), 0)
    )

    nil
  end


  contents = VBox(
    HBox(
      # combo box label
      Label(_("Current Configuration")),
      ComboBox(Id(:configs), Opt(:notify), "", Builtins.maplist(configs) do |config|
        Item(Id(config), config, config == Snapper.current_config)
      end),
      HStretch()
    ),
    Table(
      Id(:snapshots_table),
      Opt(:notify, :keepSorting),
      Header(
        # table header
        _("ID"),
        _("Type"),
        _("Start Date"),
        _("End Date"),
        _("Description"),
        _("User Data")
      ),
      get_snapshot_items.call
    ),
    HBox(
      # button label
      PushButton(Id(:show), Opt(:default), _("Show Changes")),
      PushButton(Id(:create), Label.CreateButton),
      # button label
      PushButton(Id(:modify), _("Modify")),
      PushButton(Id(:delete), Label.DeleteButton),
      HStretch()
    )
  )

  Wizard.SetContentsButtons(
    caption,
    contents,
    Ops.get_string(@HELPS, "summary", ""),
    Label.BackButton,
    Label.CloseButton
  )
  Wizard.HideBackButton
  Wizard.HideAbortButton

  UI.SetFocus(Id(:snapshots_table))
  UI.ChangeWidget(Id(:show), :Enabled, false) if snapshot_items == []
  UI.ChangeWidget(
    Id(:configs),
    :Enabled,
    Ops.greater_than(Builtins.size(configs), 1)
  )

  ret = nil
  while true
    ret = UI.UserInput

    selected = Convert.to_integer(
      UI.QueryWidget(Id(:snapshots_table), :CurrentItem)
    )

    if ret == :abort || ret == :cancel || ret == :back
      if ReallyAbort()
        break
      else
        next
      end
    elsif ret == :show
      if Ops.get(snapshots, [selected, "type"]) == :PRE
        # popup message
        Popup.Message(
          _(
            "This 'Pre' snapshot is not paired with any 'Post' one yet.\nShowing differences is not possible."
          )
        )
        next
      end
      # `POST snapshot is selected from the couple
      Snapper.selected_snapshot = Ops.get(snapshots, selected, {})
      Snapper.selected_snapshot_index = selected
      break
    elsif ret == :configs
      config = Convert.to_string(UI.QueryWidget(Id(ret), :Value))
      if config != Snapper.current_config
        Snapper.current_config = config
        update_snapshots.call
        next
      end
    elsif ret == :create
      if CreateSnapshotPopup(pre_snapshots)
        update_snapshots.call
        next
      end
    elsif ret == :modify
      if ModifySnapshotPopup(Ops.get(snapshots, selected, {}))
        update_snapshots.call
        next
      end
    elsif ret == :delete
      if DeleteSnapshotPopup(Ops.get(snapshots, selected, {}))
        update_snapshots.call
        next
      end
    elsif ret == :next
      break
    else
      Builtins.y2error("unexpected retcode: %1", ret)
      next
    end
  end

  deep_copy(ret)
end

- (Object) timestring(t)



47
48
49
# File '../../src/include/snapper/dialogs.rb', line 47

def timestring(t)
  return Time.at(t).strftime("%F %T")
end

- (Object) userdata2string(userdata)

convert map of userdata to string $[ “a” : “b”, “1” : “2” ] -> “a=b,1=2”



68
69
70
71
72
73
# File '../../src/include/snapper/dialogs.rb', line 68

def userdata2string(userdata)
  userdata = deep_copy(userdata)
  Builtins.mergestring(Builtins.maplist(userdata) do |key, val|
    Builtins.sformat("%1=%2", key, val)
  end, ",")
end