Sha256: 2c09546fc6bdbabb482d6868d43c418318380187e8689b87109d5c0da6f74982

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8

module Imap::Backup
  module Configuration; end

  class Configuration::FolderChooser
    attr_reader :account

    def initialize(account)
      @account = account
    end

    def run
      if connection.nil?
        Imap::Backup.logger.warn 'Connection failed'
        highline.ask 'Press a key '
        return
      end

      if folders.nil?
        Imap::Backup.logger.warn 'Unable to get folder list'
        highline.ask 'Press a key '
        return
      end

      catch :done do
        loop do
          system('clear')
          show_menu
        end
      end
    end

    private

    def show_menu
      highline.choose do |menu|
        menu.header = 'Add/remove folders'
        menu.index = :number
        add_folders menu
        menu.choice('return to the account menu') { throw :done }
        menu.hidden('quit') { throw :done }
      end
    end

    def add_folders(menu)
      folders.each do |folder|
        name = folder.name
        mark = is_selected?(name) ? '+' : '-'
        menu.choice("#{mark} #{name}") do
          toggle_selection name
        end
      end
    end

    def is_selected?(folder_name)
      account[:folders].find { |f| f[:name] == folder_name }
    end

    def toggle_selection(folder_name)
      if is_selected?(folder_name)
        changed = account[:folders].reject! { |f| f[:name] == folder_name }
        account[:modified] = true if changed
      else
        account[:folders] << { :name => folder_name }
        account[:modified] = true
      end
    end

    def connection
      @connection ||= Account::Connection.new(account)
    rescue => e
      nil
    end

    def folders
      @folders ||= connection.folders
    end

    def highline
      Configuration::Setup.highline
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
imap-backup-1.2.2 lib/imap/backup/configuration/folder_chooser.rb
imap-backup-1.2.1 lib/imap/backup/configuration/folder_chooser.rb
imap-backup-1.2.0 lib/imap/backup/configuration/folder_chooser.rb
imap-backup-1.1.0 lib/imap/backup/configuration/folder_chooser.rb
imap-backup-1.0.17 lib/imap/backup/configuration/folder_chooser.rb
imap-backup-1.0.16 lib/imap/backup/configuration/folder_chooser.rb
imap-backup-1.0.15 lib/imap/backup/configuration/folder_chooser.rb