Module: Lich::Common::GUI::Utilities

Defined in:
documented/common/gui/utilities.rb

Class Method Summary collapse

Class Method Details

.create_button_css_provider(font_size: 12) ⇒ Gtk::CssProvider

Creates a CSS provider for buttons with a specified font size.

Parameters:

  • font_size (Integer) (defaults to: 12)

    the font size for the button text (default is 12)

Returns:

  • (Gtk::CssProvider)

    the CSS provider for buttons



10
11
12
13
14
# File 'documented/common/gui/utilities.rb', line 10

def self.create_button_css_provider(font_size: 12)
  css = Gtk::CssProvider.new
  css.load_from_data("button {border-radius: 5px; font-size: #{font_size}px;}")
  css
end

.create_message_dialog(parent: nil, icon: nil) ⇒ Proc

Creates a message dialog with a specified parent and icon.

Parameters:

  • parent (Gtk::Window, nil) (defaults to: nil)

    the parent window for the dialog

  • icon (Gdk::Pixbuf, nil) (defaults to: nil)

    the icon to display in the dialog

Returns:

  • (Proc)

    a lambda that takes a message and displays the dialog



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'documented/common/gui/utilities.rb', line 29

def self.create_message_dialog(parent: nil, icon: nil)
  ->(message) {
    dialog = Gtk::MessageDialog.new(
      parent: parent,
      flags: :modal,
      type: :info,
      buttons: :ok,
      message: message
    )
    dialog.title = "Message"
    dialog.set_icon(icon) if icon
    dialog.run
    dialog.destroy
  }
end

.create_tab_css_providerGtk::CssProvider

Creates a CSS provider for tabs.

Returns:

  • (Gtk::CssProvider)

    the CSS provider for tabs



18
19
20
21
22
# File 'documented/common/gui/utilities.rb', line 18

def self.create_tab_css_provider
  css = Gtk::CssProvider.new
  css.load_from_data("notebook {border-width: 1px; border-color: #999999; border-style: solid;}")
  css
end

.game_code_to_realm(game_code) ⇒ String

Converts a game code to its corresponding realm name.

Parameters:

  • game_code (String)

    the game code to convert

Returns:

  • (String)

    the corresponding realm name or the original game code if not found



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'documented/common/gui/utilities.rb', line 49

def self.game_code_to_realm(game_code)
  case game_code
  when "GS3"
    "GS Prime"
  when "GSF"
    "GS Shattered"
  when "GSX"
    "GS Platinum"
  when "GST"
    "GS Test"
  when "DR"
    "DR Prime"
  when "DRF"
    "DR Fallen"
  when "DRT"
    "DR Test"
  else
    game_code
  end
end

.realm_to_game_code(realm) ⇒ String

Converts a realm name to its corresponding game code.

Parameters:

  • realm (String)

    the realm name to convert

Returns:

  • (String)

    the corresponding game code or "GS3" if not found



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'documented/common/gui/utilities.rb', line 74

def self.realm_to_game_code(realm)
  case realm.downcase
  when "gemstone iv", "prime"
    "GS3"
  when "gemstone iv shattered", "shattered"
    "GSF"
  when "gemstone iv platinum", "platinum"
    "GSX"
  when "gemstone iv prime test", "test"
    "GST"
  when "dragonrealms", "dr prime"
    "DR"
  when "dragonrealms the fallen", "dr fallen"
    "DRF"
  when "dragonrealms prime test", "dr test"
    "DRT"
  else
    "GS3" # Default to GS3 if unknown
  end
end

.safe_file_operation(file_path, operation, content = nil) ⇒ String, Boolean

Performs a safe file operation (read, write, or backup) with error handling.

Parameters:

  • file_path (String)

    the path to the file

  • operation (Symbol)

    the operation to perform (:read, :write, or :backup)

  • content (String, nil) (defaults to: nil)

    the content to write (only for :write)

Returns:

  • (String, Boolean)

    the file content for :read, true for successful write, false for backup failure



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'documented/common/gui/utilities.rb', line 101

def self.safe_file_operation(file_path, operation, content = nil)
  case operation
  when :read
    File.read(file_path)
  when :write
    # Create backup if file exists
    safe_file_operation(file_path, :backup) if File.exist?(file_path)

    # Write content to file with secure permissions
    File.open(file_path, 'w', 0600) do |file|
      file.write(content)
    end
    true
  when :backup
    return false unless File.exist?(file_path)

    backup_file = "#{file_path}.bak"
    FileUtils.cp(file_path, backup_file)
    true
  end
rescue StandardError => e
  Lich.log "error: Error in file operation (#{operation}): #{e.message}"
  operation == :read ? "" : false
end

.sort_entries(entries, autosort_state) ⇒ Array<Hash>

Sorts a list of entries based on the autosort state.

Parameters:

  • entries (Array<Hash>)

    the entries to sort

  • autosort_state (Boolean)

    whether to sort automatically or not

Returns:

  • (Array<Hash>)

    the sorted entries



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'documented/common/gui/utilities.rb', line 169

def self.sort_entries(entries, autosort_state)
  if autosort_state
    # Sort by game name, account name, and character name
    entries.sort do |a, b|
      [a[:game_name], a[:user_id], a[:char_name]] <=> [b[:game_name], b[:user_id], b[:char_name]]
    end
  else
    # Sort by account name and character name (old Lich 4 style)
    entries.sort do |a, b|
      [a[:user_id].downcase, a[:char_name]] <=> [b[:user_id].downcase, b[:char_name]]
    end
  end
end

.verified_file_operation(file_path, operation, content = nil) ⇒ String, Boolean

Performs a verified file operation (read or write) with error handling and verification.

Parameters:

  • file_path (String)

    the path to the file

  • operation (Symbol)

    the operation to perform (:read or :write)

  • content (String, nil) (defaults to: nil)

    the content to write (only for :write)

Returns:

  • (String, Boolean)

    the file content for :read, true if write was successful, false otherwise



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
# File 'documented/common/gui/utilities.rb', line 132

def self.verified_file_operation(file_path, operation, content = nil)
  case operation
  when :read
    File.read(file_path)
  when :write
    # Create backup if file exists
    safe_file_operation(file_path, :backup) if File.exist?(file_path)

    # Write content with forced synchronization and secure permissions
    File.open(file_path, 'w', 0600) do |file|
      file.write(content)
      file.flush    # Force write to OS buffer
      file.fsync    # Force OS to write to disk
    end

    # Verify write completed by reading back and comparing
    written_content = File.read(file_path)
    return written_content == content
  when :backup
    return false unless File.exist?(file_path)

    backup_file = "#{file_path}.bak"
    FileUtils.cp(file_path, backup_file)

    # Verify backup was created successfully
    File.exist?(backup_file) && File.size(backup_file) == File.size(file_path)
  end
rescue StandardError => e
  Lich.log "error: Error in verified file operation (#{operation}): #{e.message}"
  operation == :read ? "" : false
end