Sha256: 217aeb29fb1dbf457c4026b46a8a3fca06843426d7bbdb3281c870a64c29c327

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

require 'ppds/config'
require 'ppds/flickr'
require 'fliewr/ui'
require 'gtk2'

module Fliewr
  class Core

    def initialize
      $app = self
      $cfg = Ppds::Config.new 'fliewr'

      $gui = Fliewr::UI.new
      $gui.show_all

      $api = Ppds::Flickr.new

      start_timer
    end

    def update
      Gtk.timeout_remove(@@timeout) if @@timeout
      $gui.statusbar.push 0, "updating..."
      
      Thread.new do
        ask_update $cfg.get(:update_interval) * 60
        photos = $api.update $cfg.get(:max_photos)
        $gui.refresh_with photos
        @last_updated_at = timestamp
      end
      start_timer
    rescue Exception => e
      Alert.new @nsid, e.message, e.backtrace
    end

    def timestamp
      Time.now.to_i
    end

    def ask_update(time_ahead = 0)
      @requires_update_at = timestamp + time_ahead
    end

    def start_timer
      @@timeout = Gtk::timeout_add(1000) do
        sec_to_next_update = @requires_update_at.to_i - timestamp
        if sec_to_next_update < 0
          update
        else
          $gui.statusbar.push 0, "%s to update" % human_time(sec_to_next_update)
        end
      end
    end

    def human_time(time)
      seconds = time.modulo(SECONDS_PER_MINUTE)
      minutes = (time - seconds) / SECONDS_PER_MINUTE
      "%d:%02d" % [minutes, seconds]
    end

    def main
      Gtk::main
    end

    def quit
      Gtk::main_quit
    ensure
      $cfg.save
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
themactep-fliewr-2.0.0 lib/fliewr/core.rb
themactep-fliewr-2.0.1 lib/fliewr/core.rb
themactep-fliewr-2.0.2 lib/fliewr/core.rb
themactep-fliewr-2.1.0 lib/fliewr/core.rb
themactep-fliewr-2.1.1 lib/fliewr/core.rb
fliewr-2.0.0 lib/fliewr/core.rb