# program_item.rb

# $Id: program_item.rb,v 1.2 2002/05/04 09:32:01 makeinu Exp $

# 2002/4/30  Takuro Ashie <ashie@homa.ne.jp>
# Copyright (C) 2002 Takuro Ashie <ashie@homa.ne.jp>
# This is free software with ABSOLUTELY NO WARRANTY.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


require 'kconv'

#iEPG Cannels definition
IepgChannelsFile = VideoRecordingConfDir + "/channel_iepg.conf"


###############################################################################
#
#   ProgramItem Class
#
###############################################################################
class ProgramItem

  #
  # initialize()
  #
  def initialize
    @starttime = nil
    @endtime   = nil
    @channel   = nil
    @quality   = nil
    @filename  = nil
  end

  attr_accessor :starttime    #Time
  attr_accessor :endtime      #Time
  attr_accessor :channel      #String
  attr_accessor :quality      #String
  attr_accessor :filename     #String


  #
  # ==() :
  #    Return true when each record is completely in agreement.
  #
  # aItem   : The ProgramItem to compare.
  # Return  : true when both are completely in agreement.
  #
  def ==(aItem)
    if starttime != aItem.starttime then
      return false
    end

    if endtime != aItem.endtime then
      return false
    end

    if channel != aItem.channel then
      return false
    end

    if quality != aItem.quality then
      return false
    end

    if filename != aItem.filename then
      return false
    end

    return true
  end


  #
  # valid?() :
  #    check for inaccurate data.
  #
  # Return : true if inaccurate data is exist.
  #
  def valid?()
    if !starttime || !endtime || !channel || !quality || !filename then
      return false
    end

    if channel == "" || quality == "" || filename == "" then
      return false
    end

    # if starttime < Time.now || endtime < Time.now
    #    return false
    # end

    if starttime >= endtime then
      return false
    end

    # FIXME!! Should we return false if channel or quality value is not exist?

    return true
  end


  #
  # now?() :
  #    Check whether recording (maybe) or not.
  #
  # Return : true when the present time is in reservation time zone.
  #
  def now?()
    if starttime < Time.now && Time.now < endtime then
      return true
    else
      return false
    end
  end


  #
  # running?() :
  #
  # Return : true if the recording proccess is running.
  #
  # def running?()
  # end


  #
  # obsolete?() :
  #    Check whether the program is already ended or not.
  #
  # Return : true when the end time is before the present time 
  #
  def obsolete?()
    if endtime < Time.now then
      return true
    else
      return false
    end
  end


  #
  # doubleBooking? () :
  #    Check whether a reservation time zone is overlaping to already registered
  #    reservation.
  #
  # aProgram : The ProgramItem to compare¡¥
  # Return   : true if aProgram time zone overlaps to other program¡¥
  #
  def doubleBooking?(aProgram)
    if starttime >= endtime then
      return false
    end

    if aProgram.starttime >= aProgram.endtime then
      return false
    end

    if starttime < aProgram.starttime then
      stime = aProgram.starttime
    else
      stime = starttime
    end

    if endtime < aProgram.endtime then
      etime = endtime
    else
      etime = aProgram.endtime
    end

    if stime > etime then
      return false
    else
      return true
    end
  end
end



###############################################################################
#
#   IEPG Class
#
###############################################################################
class IEPG < ProgramItem
  include Kconv

  @@iepg_channels = nil    #Hash

  #
  # initialize()
  #
  def initialize(aStr)
    loadIepgChannels

    @iepg_raw_data = aStr  # String
    @title         = nil   # String
    @description   = nil   # String

    parse
  end

  attr_reader :title        #String
  attr_reader :description  #String

 private

  #
  # loadIepgChannels() :
  #    load iEPG channels definition from file.
  #
  def loadIepgChannels()
    if @@iepg_channels || !File::exist?(IepgChannelsFile) then
      retrun
    end

    @@iepg_channels = Hash::new

    open (IepgChannelsFile) do |file|
      file.each do |line|
        next unless (line =~ /(.+)\s+(.+)/)
        @@iepg_channels[$1.strip] = $2.strip
      end
    end
  end

  #
  # parse() :
  #    translate iEPG data string to instance valiables.
  #
  def parse()

    year     = nil
    month    = nil
    date     = nil
    start_hm = nil  # Array for start hour and min
    end_hm   = nil  # Array for end   hour and min
    title    = nil

    is_description  = false

    @iepg_raw_data.each_line do |line|
      if is_description then
        if !@description
          @description = ""
        end
        @description += line
      else
        if !(line =~ /(.*?):(.*)/)
          is_description = true
          next
        end

        key = $1.strip
        value = $2.strip

        case key
        when "station"
          @station = toeuc(value);
        when "year"
          year = value
        when "month"
          month = value
        when "date"
          date = value
        when "start"
          start_hm = value.split( /:/ )
        when "end"
          end_hm = value.split( /:/ )
        when "program-title"
          title = toeuc(value)
        end
      end
    end

    # FIXME!! should be added exception handling?

    @starttime = Time.local(year.to_i, month.to_i, date.to_i,
                            start_hm[0].to_i, start_hm[1].to_i, 0)
    @endtime   = Time.local(year.to_i, month.to_i, date.to_i,
                            end_hm[0].to_i, end_hm[1].to_i, 0)

    if @@iepg_channels then
      @channel = @@iepg_channels[@station]
    end

    #@channels = ChannelDefs[@station]

    if title then
      @filename = title + ".avi"
    end

  end
end
