Module: Lich::Common::GUI::State

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

Class Method Summary collapse

Class Method Details

.apply_theme_settings(theme_state) ⇒ void

This method returns an undefined value.

Applies theme settings based on the provided state.

Parameters:

  • theme_state (Boolean)

    true to prefer dark theme, false otherwise



58
59
60
# File 'documented/common/gui/state.rb', line 58

def self.apply_theme_settings(theme_state)
  Gtk::Settings.default.gtk_application_prefer_dark_theme = true if theme_state == true
end

.load_saved_entries(data_dir, autosort_state) ⇒ Array<Hash>

Loads saved entries from a specified data directory.

Examples:

Load entries with autosorting

entries = Lich::Common::GUI::State.load_saved_entries("/path/to/data", true)

Parameters:

  • data_dir (String)

    the directory where entry.dat is located

  • autosort_state (Boolean)

    whether to sort entries by instance name, account name, and character name

Returns:

  • (Array<Hash>)

    sorted array of loaded entries or an empty array if loading fails



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'documented/common/gui/state.rb', line 13

def self.load_saved_entries(data_dir, autosort_state)
  if File.exist?(File.join(data_dir, "entry.dat"))
    File.open(File.join(data_dir, "entry.dat"), 'r') { |file|
      begin
        if autosort_state
          # Sort in list by instance name, account name, and then character name
          Marshal.load(file.read.unpack('m').first).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 in list by account name, and then character name (old Lich 4)
          Marshal.load(file.read.unpack('m').first).sort do |a, b|
            [a[:user_id].downcase, a[:char_name]] <=> [b[:user_id].downcase, b[:char_name]]
          end
        end
      rescue
        Array.new
      end
    }
  else
    Lich.log "Info: No entry.dat file detected, probable new installation."
    Array.new
  end
end

.save_entries(data_dir, entry_data) ⇒ Boolean

Saves entries to a specified data directory.

Examples:

Save entries

success = Lich::Common::GUI::State.save_entries("/path/to/data", entries)

Parameters:

  • data_dir (String)

    the directory where entry.dat will be saved

  • entry_data (Array<Hash>)

    the entries to save

Returns:

  • (Boolean)

    true if saving was successful, false otherwise



45
46
47
48
49
50
51
52
# File 'documented/common/gui/state.rb', line 45

def self.save_entries(data_dir, entry_data)
  File.open(File.join(data_dir, "entry.dat"), 'w') { |file|
    file.write([Marshal.dump(entry_data)].pack('m'))
  }
  true
rescue
  false
end