Class: Yast::GPGWidgetsClass

Inherits:
Module
  • Object
show all
Defined in:
library/gpg/src/modules/GPGWidgets.rb

Instance Method Summary collapse

Instance Method Details

#AskPassphrasePopup(key) ⇒ String

Ask user to enter the passphrase for the selected gpg key. A a popup window is displayed.

Parameters:

  • key (String)

    key ID of the gpg key

Returns:

  • (String)

    the entered passphrase or nil if the popup has been closed by [Cancel] button



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
# File 'library/gpg/src/modules/GPGWidgets.rb', line 373

def AskPassphrasePopup(key)
  @passphrase = nil

  if Mode.commandline
    # no input possible
    return nil unless CommandLine.Interactive

    # ask for the passphrase in the commandline (interactive) mode
    return CommandLine.PasswordInput(
      Builtins.sformat(_("Enter Passphrase to Unlock GPG Key %1: "), key)
    )
  end

  # run the dialog
  w = CWM.CreateWidgets(["ask_passphrase"], AskPassphraseWidget(key))

  contents = AskPassphraseTerm()
  contents = CWM.PrepareDialog(contents, w)

  UI.OpenDialog(contents)
  UI.SetFocus(Id(:passphrase))
  CWM.Run(w, {})
  UI.CloseDialog

  @passphrase
end

#AskPassphraseTermYast::Term

Create a popup window term with the passphrase widget.

Returns:

  • (Yast::Term)

    definition of the popup



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
# File 'library/gpg/src/modules/GPGWidgets.rb', line 342

def AskPassphraseTerm
  MarginBox(
    term(:leftMargin, 1),
    term(:rightMargin, 1),
    term(:topMargin, 0.2),
    term(:bottomMargin, 0.5),
    VBox(
      HSpacing(50),
      Heading(_("Enter Passphrase")),
      "ask_passphrase",
      VSpacing(0.5),
      ButtonBox(
        PushButton(
          Id(:ok),
          Opt(:default, :okButton, :key_F10),
          Label.OKButton
        ),
        PushButton(
          Id(:cancel),
          Opt(:cancelButton, :key_F9),
          Label.CancelButton
        )
      )
    )
  )
end

#AskPassphraseWidget(key) ⇒ Hash{String => map<String,Object>}

Return definition of the passphrase CWM widget.

Parameters:

  • key (String)

    key ID displayed in the label

Returns:

  • (Hash{String => map<String,Object>})

    widget definition



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'library/gpg/src/modules/GPGWidgets.rb', line 317

def AskPassphraseWidget(key)
  {
    "ask_passphrase" => {
      "widget"        => :custom,
      "custom_widget" => VBox(
        # text entry
        Password(
          Id(:passphrase),
          Builtins.sformat(_("&Passphrase for GPG Key %1"), key)
        )
      ),
      "store"         => fun_ref(
        method(:PassphraseStore),
        "void (string, map)"
      ),
      # help text
      "help"          => _(
        "<p><big><b>Passphrase</b></big><br>\nEnter passphrase to unlock the GPG key."
      )
    }
  }
end

#CreateNewKeyObject

Get widget description map

Returns:

  • widget description map



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'library/gpg/src/modules/GPGWidgets.rb', line 279

def CreateNewKey
  {
    "widget"        => :push_button,
    "label"         => _("&Create a new GPG key..."),
    "handle_events" => ["create_new_key"],
    "handle"        => fun_ref(method(:GpgNewKey), "symbol (string, map)"),
    "help"          => _(
      "<p><big><b>Create a new GPG key</b></big><br>\n" \
        "<tt>gpg --gen-key</tt> is started, see <tt>gpg</tt> manual pager for more information.\n" \
        "Press Ctrl+C to cancel.\n" \
        "</p>"
    )
  }
end

#GpgInitPrivate(key) ⇒ Object

Init function of a widget - initialize the private table widget

Parameters:

  • key (String)

    string widget key



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'library/gpg/src/modules/GPGWidgets.rb', line 97

def GpgInitPrivate(key)
  Builtins.y2milestone("GpgInitPrivate: %1", key)

  if key == "select_private_key"
    UI.ChangeWidget(Id(:gpg_priv_table), :Items, GPGItems(true))

    if !@_selected_id_private_key.nil?
      UI.ChangeWidget(
        Id(:gpg_priv_table),
        :CurrentItem,
        @_selected_id_private_key
      )
    end
  end

  nil
end

#GpgInitPublic(key) ⇒ Object

Init function of a widget - initialize the public table widget

Parameters:

  • key (String)

    string widget key



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'library/gpg/src/modules/GPGWidgets.rb', line 117

def GpgInitPublic(key)
  Builtins.y2milestone("GpgInitPublic: %1", key)

  if key == "select_public_key"
    UI.ChangeWidget(Id(:gpg_public_table), :Items, GPGItems(false))

    if !@_selected_id_public_key.nil?
      UI.ChangeWidget(
        Id(:gpg_public_table),
        :CurrentItem,
        @_selected_id_public_key
      )
    end
  end

  nil
end

#GPGItems(private_keys) ⇒ Array<Yast::Term>

Get list of table items for CWM widget.

Parameters:

  • private_keys (Boolean)

    if true use private keys, otherwise use public keys

Returns:

  • (Array<Yast::Term>)

    list of items



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'library/gpg/src/modules/GPGWidgets.rb', line 75

def GPGItems(private_keys)
  ret = []
  keys = private_keys ? GPG.PrivateKeys : GPG.PublicKeys

  Builtins.foreach(keys) do |key|
    uids = Builtins.mergestring(Ops.get_list(key, "uid", []), ", ")
    ret = Builtins.add(
      ret,
      Item(
        Id(Ops.get_string(key, "id", "")),
        Ops.get_string(key, "id", ""),
        uids,
        Ops.get_string(key, "fingerprint", "")
      )
    )
  end

  deep_copy(ret)
end

#GpgNewKey(key, event) ⇒ Object

Refresh the widgets after creating a new gpg key

Parameters:

  • key (String)

    widget ID

  • event (Hash)

    event



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
# File 'library/gpg/src/modules/GPGWidgets.rb', line 248

def GpgNewKey(key, event)
  event = deep_copy(event)
  Builtins.y2debug("GpgNewKey: %1, %2", key, event)

  if key == "create_new_key"
    GPG.CreateKey

    # refresh private key widget if it's existing
    if UI.WidgetExists(Id(:gpg_priv_table))
      current = Convert.to_string(
        UI.QueryWidget(Id(:gpg_priv_table), :CurrentItem)
      )
      UI.ChangeWidget(Id(:gpg_priv_table), :Items, GPGItems(true))
      UI.ChangeWidget(Id(:gpg_priv_table), :CurrentItem, current)
    end

    # refresh public key widget if it's existing
    if UI.WidgetExists(Id(:gpg_public_table))
      current = Convert.to_string(
        UI.QueryWidget(Id(:gpg_public_table), :CurrentItem)
      )
      UI.ChangeWidget(Id(:gpg_public_table), :Items, GPGItems(false))
      UI.ChangeWidget(Id(:gpg_public_table), :CurrentItem, current)
    end
  end

  nil
end

#GpgStorePrivate(key, event) ⇒ Object

Store the selected private key

Parameters:

  • key (String)

    widget ID

  • event (Hash)

    event



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'library/gpg/src/modules/GPGWidgets.rb', line 138

def GpgStorePrivate(key, event)
  event = deep_copy(event)
  Builtins.y2debug("GpgStorePrivate: %1, %2", key, event)

  if key == "select_private_key"
    @_selected_id_private_key = Convert.to_string(
      UI.QueryWidget(Id(:gpg_priv_table), :CurrentItem)
    )
    Builtins.y2milestone(
      "Selected private key: %1",
      @_selected_id_private_key
    )
  end

  nil
end

#GpgStorePublic(key, event) ⇒ Object

Store the selected public key

Parameters:

  • key (String)

    widget ID

  • event (Hash)

    event



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'library/gpg/src/modules/GPGWidgets.rb', line 158

def GpgStorePublic(key, event)
  event = deep_copy(event)
  Builtins.y2debug("GpgStorePublic: %1, %2", key, event)

  if key == "select_public_key"
    @_selected_id_public_key = Convert.to_string(
      UI.QueryWidget(Id(:gpg_public_table), :CurrentItem)
    )
    Builtins.y2milestone(
      "Selected public key: %1",
      @_selected_id_public_key
    )
  end

  nil
end

#mainObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'library/gpg/src/modules/GPGWidgets.rb', line 36

def main
  Yast.import "UI"

  Yast.import "Mode"
  Yast.import "GPG"
  Yast.import "Label"
  Yast.import "CWM"
  Yast.import "CommandLine"

  textdomain "base"

  # the selected private key in the private key table
  @_selected_id_private_key = nil
  # the selected public key in the public key table
  @_selected_id_public_key = nil

  # Passphrase entered in the passphrase widget
  @passphrase = ""
end

#PassphraseObject

Get the enterd passphrase.

Returns:

  • passphrase



310
311
312
# File 'library/gpg/src/modules/GPGWidgets.rb', line 310

def Passphrase
  @passphrase
end

#PassphraseStore(key, event) ⇒ Object

Store the passphrase from the widget

Parameters:

  • key (String)

    widget ID

  • event (Hash)

    event



297
298
299
300
301
302
303
304
305
306
# File 'library/gpg/src/modules/GPGWidgets.rb', line 297

def PassphraseStore(key, event)
  event = deep_copy(event)
  Builtins.y2debug("PassphraseStore: %1, %2", key, event)

  if Ops.get_symbol(event, "WidgetID", :_none) == :ok
    @passphrase = Convert.to_string(UI.QueryWidget(Id(:passphrase), :Value))
  end

  nil
end

#PrivateKeySelectionObject

Get widget description map

Returns:

  • widget description map



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
# File 'library/gpg/src/modules/GPGWidgets.rb', line 183

def PrivateKeySelection
  {
    "widget"        => :custom,
    "custom_widget" => VBox(
      Left(Label(Id(:gpg_priv_label), _("GPG Private Keys"))),
      Table(
        Id(:gpg_priv_table),
        # table header - GPG key ID
        Header(
          _("Key ID"),
          # table header - GPG key user ID
          _("User ID"),
          # table header - GPG key fingerprint
          _("Fingerprint")
        ),
        # fill up the widget in init handler
        []
      )
    ),
    "init"          => fun_ref(method(:GpgInitPrivate), "void (string)"),
    "store"         => fun_ref(
      method(:GpgStorePrivate),
      "void (string, map)"
    ),
    "help"          => _(
      "<p><big><b>GPG Private Key</b></big><br>\nThe table contains list of the private GPG keys.</p>"
    )
  }
end

#PublicKeySelectionObject

Get widget description map

Returns:

  • widget description map



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
# File 'library/gpg/src/modules/GPGWidgets.rb', line 215

def PublicKeySelection
  {
    "widget"        => :custom,
    "custom_widget" => VBox(
      Left(Label(_("GPG Public Keys"))),
      Table(
        Id(:gpg_public_table),
        # table header - GPG key ID
        Header(
          _("Key ID"),
          # table header - GPG key user ID
          _("User ID"),
          # table header - GPG key fingerprint
          _("Fingerprint")
        ),
        # fill up the widget in init handler
        []
      )
    ),
    "init"          => fun_ref(method(:GpgInitPublic), "void (string)"),
    "store"         => fun_ref(
      method(:GpgStorePublic),
      "void (string, map)"
    ),
    "help"          => _(
      "<p><big><b>GPG Public Key</b></big><br>\nThe table contains list of the public GPG keys.</p>"
    )
  }
end

#SelectedPrivateKeyString

Return the selected private key in the private table widget

Returns:



177
178
179
# File 'library/gpg/src/modules/GPGWidgets.rb', line 177

def SelectedPrivateKey
  @_selected_id_private_key
end

#SetSelectedPrivateKey(keyid) ⇒ Object

Set selected private key in the private key table widget.

Parameters:

  • keyid (String)

    ID of the selected key



58
59
60
61
62
# File 'library/gpg/src/modules/GPGWidgets.rb', line 58

def SetSelectedPrivateKey(keyid)
  @_selected_id_private_key = keyid

  nil
end

#SetSelectedPublicKey(keyid) ⇒ Object

Set selected public key in the public key table widget.

Parameters:

  • keyid (String)

    ID of the selected key



66
67
68
69
70
# File 'library/gpg/src/modules/GPGWidgets.rb', line 66

def SetSelectedPublicKey(keyid)
  @_selected_id_public_key = keyid

  nil
end

#WidgetsHash{String,map<String => Object>}

Return a map with CWM widgets definition. The map contains definitions of all static CWM widgets.

Returns:



402
403
404
405
406
407
408
# File 'library/gpg/src/modules/GPGWidgets.rb', line 402

def Widgets
  {
    "select_private_key" => PrivateKeySelection(),
    "select_public_key"  => PublicKeySelection(),
    "create_new_key"     => CreateNewKey()
  }
end