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


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

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


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

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


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'library/gpg/src/modules/GPGWidgets.rb', line 313

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 the passphrase to unlock the GPG key."
      )
    }
  }
end

#CreateNewKeyObject

Get widget description map

Returns:

  • widget description map


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

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 the <tt>gpg</tt> manual page 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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


306
307
308
# File 'library/gpg/src/modules/GPGWidgets.rb', line 306

def Passphrase
  @passphrase
end

#PassphraseStore(key, event) ⇒ Object

Store the passphrase from the widget

Parameters:

  • key (String)

    widget ID

  • event (Hash)

    event


295
296
297
298
299
300
301
302
# File 'library/gpg/src/modules/GPGWidgets.rb', line 295

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

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

  nil
end

#PrivateKeySelectionObject

Get widget description map

Returns:

  • widget description map


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

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 a list of private GPG keys.</p>"
    )
  }
end

#PublicKeySelectionObject

Get widget description map

Returns:

  • widget description map


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

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 a list of public GPG keys.</p>"
    )
  }
end

#SelectedPrivateKeyString

Return the selected private key in the private table widget

Returns:


175
176
177
# File 'library/gpg/src/modules/GPGWidgets.rb', line 175

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


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

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


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

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:


398
399
400
401
402
403
404
# File 'library/gpg/src/modules/GPGWidgets.rb', line 398

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