# $Id: ext_siteconfig.rb,v 1.4 2004/05/09 09:23:49 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

require 'qwik/ext_wikidb'

module Qwik
  class Site
    def siteconfig()
      @siteconfig = SiteConfig.new(@config, self) if @siteconfig.nil?
      @siteconfig
    end

    def is_open?
      siteconfig()["open"] == "true"
    end

    def title(pagename=nil)
      t = siteconfig()["sitename"]
      t = site_title() if t == 'qwik.jp'
      pt = page_title(pagename)
      t = pt+" - "+t if pt
      t
    end
  end

  class SiteConfig
    def initialize(config, site)
      @config = config
      @site = site
      @default = {
	"open" => "false",
	"theme" => @config.default_theme,
	"sitename" => 'qwik.jp',
      }
      @key = @config.siteconfig
    end

    def [](k)
      v = db[k]
      return @default[k] if v.nil?
      v
    end

    private
    def db
      page = @site[@key]
      page = @site.create(@key) if page.nil?
      page.wikidb
    end

  end
end
