use utf8;
use constant TRUE  => 1;
use constant FALSE => 0;
use URI::file;

sub gnFirstRun {
   # 初回起動時にウィザードを起動して、gikoNavi.iniとログディレクトリを
   # 指定する
   #
   # 引数: なし
   # 返り値: なし
   #my $gladefn = $_[0];
   $gnFRxml = Gtk2::GladeXML->new($gladefn, 'gnInitialConfig');
   $gnFRxml->signal_autoconnect_from_package('main');
   my $gnFirstRunGUI = $gnFRxml->get_widget('gnInitialConfig');
   if($gnDebugFlag) { print Dumper($gnFirstRunGUI); }
   
   my $gnFRIniChoose = $gnFRxml->get_widget('gnFRIniChoose');
   my $gnFRIniFilter = Gtk2::FileFilter->new;
   $gnFRIniFilter->add_pattern('gikoNavi.ini');
   $gnFRIniChoose->set_filter($gnFRIniFilter);
   
   my $gnFRUrlChoose = $gnFRxml->get_widget('gnFRUrlChoose');
   my $gnFRUrlFilter = Gtk2::FileFilter->new;
   $gnFRUrlFilter->add_pattern('url.ini');
   $gnFRUrlChoose->set_filter($gnFRUrlFilter);

   my $gnFRSentChoose = $gnFRxml->get_widget('gnFRSentChoose');
   my $gnFRSentFilter = Gtk2::FileFilter->new;
   $gnFRSentFilter->add_pattern('sent.ini');
   $gnFRSentChoose->set_filter($gnFRSentFilter);

   $gnFirstRunGUI->queue_resize;
   $gnFirstRunGUI->show_all;
   
   my $gnFRpage = 0;
   
}



sub gnFR_next_L1 {
   my $gnFRnote = $gnFRxml->get_widget('gnFRnote');
   # 次のページに進む
   $gnFRnote->next_page;
   
}

sub gnFR_prev_L1 {
   my $gnFRnote = $gnFRxml->get_widget('gnFRnote');
      # 前のページに戻る
      $gnFRnote->prev_page;
}

sub gnFR_next_if {
   my $gnFR_useGikonavi = $gnFRxml->get_widget('gnFirstRun_useGikonavi');
   if ($gnFR_useGikonavi->get_active) {
      # 「既存のファイルを使用」が選択されていたら
      # 次のページに進む
      my $gnFRnote = $gnFRxml->get_widget('gnFRnote');
      $gnFRnote->next_page;
   }else{
      # 「新規に作成」が選択されていたら
      # 新規作成のフローに進む
      my $gnFRnote = $gnFRxml->get_widget('gnFRnote');
      #8ページ目に進む
      $gnFRnote->set_current_page(7);
   
   }
   
}

sub gnFR_prev_if {
      my $gnFRnote = $gnFRxml->get_widget('gnFRnote');
      #8ページ目に進む
      $gnFRnote->set_current_page(1);
   
}

sub gnFR_next_L2 {
   my $gnFRnote = $gnFRxml->get_widget('gnFRnote2');
   # 次のページに進む
   $gnFRnote->next_page;
   
}

sub gnFR_prev_L2 {
   my $gnFRnote = $gnFRxml->get_widget('gnFRnote2');
      # 前のページに戻る
      $gnFRnote->prev_page;
}

sub on_gnFirstrun_exit1_clicked {
      my $gnFirstRunGUI = $gnFRxml->get_widget('gnInitialConfig');
      my $gnMsgDialog = Gtk2::MessageDialog->new($gnFirstRunGUI,
                                                    'GTK_DIALOG_MODAL',
                                                    'GTK_MESSAGE_INFO',
                                                    'GTK_BUTTONS_YES_NO',
                                                    "初期設定を中断しますか？\n（設定は保存されません）",
                                                     ,
                                                    'GTK_BUTTONS_YES' => 'gtk-yes',
                                                    'GTK_BUTTONS_NO' => 'gtk-no');
      if($gnMsgDialog->run eq 'yes') {
          $gnMsgDialog->destroy;
          undef($gnMsgDialog);
          exit(0);
      }else{
          $gnMsgDialog->destroy;
          undef($gnMsgDialog);
      }
}

sub gnFR_finish {
   my $gnFR_useGikonavi = $gnFRxml->get_widget('gnFirstRun_useGikonavi');
   if ($gnFR_useGikonavi->get_active) {
      # 「既存のファイルを使用」した場合の終了処理
      #####################################################################
      my $gnFRIni = $gnFRxml->get_widget('gnFRIniChoose');
      $gnInitCfg{'Main'}{'gikoNavi.ini'} = $gnFRIni->get_filename;
      $gnInitCfg{'Main'}{'gikoNavi.ini'} = Glib::filename_display_name($gnInitCfg{'Main'}{'gikoNavi.ini'});

#      my $bbb = Encode::encode(&env_locale_to_encstr($ENV{'LANG'}), $aaa);
#      $gnInitCfg{'Main'}{'gikoNavi.ini'} = &env_gtkstr_to_gtkstr($gnInitCfg{'Main'}{'gikoNavi.ini'});
      if($gnDebugFlag) { print "$gnInitCfg{'Main'}{'gikoNavi.ini'}\n"; }
      
      my $gnFRUrl = $gnFRxml->get_widget('gnFRUrlChoose');
      $gnInitCfg{'Main'}{'url.ini'} = $gnFRUrl->get_filename;
      $gnInitCfg{'Main'}{'url.ini'} = Glib::filename_display_name($gnInitCfg{'Main'}{'url.ini'});
#      $gnInitCfg{'Main'}{'url.ini'} = &env_gtkstr_to_gtkstr($gnInitCfg{'Main'}{'url.ini'});
      if($gnDebugFlag) { print "$gnInitCfg{'Main'}{'url.ini'}\n"; }
      
      my $gnFRSent = $gnFRxml->get_widget('gnFRSentChoose');
      $gnInitCfg{'Main'}{'sent.ini'} = $gnFRSent->get_filename;
      $gnInitCfg{'Main'}{'sent.ini'} = Glib::filename_display_name($gnInitCfg{'Main'}{'sent.ini'});
#      $gnInitCfg{'Main'}{'sent.ini'} = &env_gtkstr_to_gtkstr($gnInitCfg{'Main'}{'sent.ini'});
      if($gnDebugFlag) { print "$gnInitCfg{'Main'}{'sent.ini'}\n"; }
      
      my $gnFRLog = $gnFRxml->get_widget('gnFRLogChoose');
      $gnInitCfg{'Main'}{'LogFolder'} = $gnFRLog->get_filename;
      $gnInitCfg{'Main'}{'LogFolder'} = Glib::filename_display_name($gnInitCfg{'Main'}{'LogFolder'});
#      $gnInitCfg{'Main'}{'LogFolder'} = &env_gtkstr_to_gtkstr($gnInitCfg{'Main'}{'LogFolder'});
      if($gnDebugFlag) { print "$gnInitCfg{'Main'}{'LogFolder'}\n"; }
      
      $gnEnvFile = $gnInitCfg{'Main'}{'gikoNavi.ini'};
      $gnURLIniFile = $gnInitCfg{'Main'}{'url.ini'};
      $gnSentIniFile = $gnInitCfg{'Main'}{'sent.ini'};
      $logdir = $gnInitCfg{'Main'}{'LogFolder'} . "\/2ch\/";
      
      
      my $logdir_1 = Encode::encode(&env_locale_to_encstr($ENV{'LANG'}), $logdir);
      if (!(-d $logdir_1)) {
             my $gnFirstRunGUI = $gnFRxml->get_widget('gnInitialConfig');
             my $gnMsgDialog = Gtk2::MessageDialog->new($gnFirstRunGUI,
                                                           'GTK_DIALOG_MODAL',
                                                           'GTK_MESSAGE_INFO',
                                                           'GTK_BUTTONS_YES_NO',
                                                           "2chディレクトリがありません。\n作成しますか？",
                                                           ,
                                                           'GTK_BUTTONS_YES' => 'gtk-yes',
                                                           'GTK_BUTTONS_NO' => 'gtk-no');
             if($gnMsgDialog->run eq 'yes') {
                $gnMsgDialog->destroy;
                undef($gnMsgDialog);
                mkdir($logdir_1);
             }else{
                $gnMsgDialog->destroy;
                undef($gnMsgDialog);
                exit(1);
             }
         
      }

      # <設定ディレクトリ>/.gnviewconfigへ書き込み
      if (!(-d $gnCfgPath)) {
             my $gnFirstRunGUI = $gnFRxml->get_widget('gnInitialConfig');
             my $gnMsgDialog = Gtk2::MessageDialog->new($gnFirstRunGUI,
                                                           'GTK_DIALOG_MODAL',
                                                           'GTK_MESSAGE_INFO',
                                                           'GTK_BUTTONS_YES_NO',
                                                           "設定ファイルを格納するディレクトリがありません。\n作成しますか？",
                                                           ,
                                                           'GTK_BUTTONS_YES' => 'gtk-yes',
                                                           'GTK_BUTTONS_NO' => 'gtk-no');
             if($gnMsgDialog->run eq 'yes') {
                $gnMsgDialog->destroy;
                undef($gnMsgDialog);
                mkdir($gnCfgPath);
             }else{
                $gnMsgDialog->destroy;
                undef($gnMsgDialog);
                exit(1);
             }
         
      }
      # 画像キャッシュテーブルの初期化
      $gnInitCfg{'Main'}{'cachetbl.ini'} = $gnCfgPath . "cachetbl.ini";
      if (!(-e $gnInitCfg{'Main'}{'cachetbl.ini'})) {
         open(gnCTBL, ">$gnInitCfg{'Main'}{'cachetbl.ini'}");
         close(gnCTBL);
      }
      
      my $gnInitCfgFN = $gnCfgPath . ".gnviewconfig";
      $gnInitCfg{'Main'}{'gnCfgPath'} = $gnCfgPath;
      if($gnDebugFlag) { print Dumper(%gnInitCfg); }
      &gnSaveEnv($gnInitCfgFN, \%gnInitCfg);
      
      my $gnFirstRunGUI = $gnFRxml->get_widget('gnInitialConfig');
      $gnFirstRunGUI->destroy;
      
      &gnInit;
      #####################################################################
   }else{
      # 「新規に作成」した場合の終了処理
      #####################################################################
      
      # <設定ディレクトリ>を作成
      if (!(-d $gnCfgPath)) {
             my $gnFirstRunGUI = $gnFRxml->get_widget('gnInitialConfig');
             my $gnMsgDialog = Gtk2::MessageDialog->new($gnFirstRunGUI,
                                                           'GTK_DIALOG_MODAL',
                                                           'GTK_MESSAGE_INFO',
                                                           'GTK_BUTTONS_YES_NO',
                                                           "設定ファイルを格納するディレクトリがありません。\n作成しますか？",
                                                           ,
                                                           'GTK_BUTTONS_YES' => 'gtk-yes',
                                                           'GTK_BUTTONS_NO' => 'gtk-no');
             if($gnMsgDialog->run eq 'yes') {
                $gnMsgDialog->destroy;
                undef($gnMsgDialog);
                mkdir($gnCfgPath);
             }else{
                $gnMsgDialog->destroy;
                undef($gnMsgDialog);
                exit(1);
             }
         
      }
      my $gngikoNaviIni_from = &gnGetLibPath . "gikoNavi.ini";
      my $gnurlIni_from = &gnGetLibPath . "url.ini";
      my $gnsentIni_from = &gnGetLibPath . "sent.ini";
      copy($gngikoNaviIni_from, $gnCfgPath);
      copy($gnurlIni_from, $gnCfgPath);
      copy($gnsentIni_from, $gnCfgPath);

      
      $gnInitCfg{'Main'}{'gikoNavi.ini'} = $gnCfgPath . "gikoNavi.ini";
      if($gnDebugFlag) { print "$gnInitCfg{'Main'}{'gikoNavi.ini'}\n"; }
      $gnInitCfg{'Main'}{'url.ini'} = $gnCfgPath . "url.ini";
      if($gnDebugFlag) { print "$gnInitCfg{'Main'}{'url.ini'}\n"; }
      $gnInitCfg{'Main'}{'sent.ini'} = $gnCfgPath . "sent.ini";
      if($gnDebugFlag) { print "$gnInitCfg{'Main'}{'sent.ini'}\n"; }

      my $gnFRLog = $gnFRxml->get_widget('gnFirstrun_LogDirChoose_init');
      if($gnDebugFlag) { print STDERR "1\n"; }
      my $aaa = $gnFRLog->get_filename;
      $aaa = Glib::filename_display_name($aaa);

      my $bbb = Encode::encode(&env_locale_to_encstr($ENV{'LANG'}), $aaa);
      if($gnDebugFlag) { print STDERR "2\: $bbb\n"; }

      if($gnDebugFlag) {
      print STDERR "3\: $bbb\n";
      if(-e $bbb) {
         print STDERR "Directory exist.\n";
      }else{
         print STDERR "Directory does\'t exist.\n";
      }
      }

      $gnInitCfg{'Main'}{'LogFolder'} = $aaa;
      if($gnDebugFlag) { print STDERR "4\: $aaa\n"; }
      if($gnDebugFlag) { print "Mail_LogFolder\: " . "$gnInitCfg{'Main'}{'LogFolder'}\n"; }

      $logdir = $gnInitCfg{'Main'}{'LogFolder'} . "\/2ch\/";
      $logdir = Encode::encode(&env_locale_to_encstr($ENV{'LANG'}), $logdir);
      if($gnDebugFlag) { print "logdir\: " . "$logdir\n"; }      
      
      $gnEnvFile = $gnInitCfg{'Main'}{'gikoNavi.ini'};
      $gnURLIniFile = $gnInitCfg{'Main'}{'url.ini'};
      $gnSentIniFile = $gnInitCfg{'Main'}{'sent.ini'};
      
      
      if (!(-d $logdir)) {
             my $gnFirstRunGUI = $gnFRxml->get_widget('gnInitialConfig');
             my $gnMsgDialog = Gtk2::MessageDialog->new($gnFirstRunGUI,
                                                           'GTK_DIALOG_MODAL',
                                                           'GTK_MESSAGE_INFO',
                                                           'GTK_BUTTONS_YES_NO',
                                                           "2chディレクトリがありません。\n作成しますか？",
                                                           ,
                                                           'GTK_BUTTONS_YES' => 'gtk-yes',
                                                           'GTK_BUTTONS_NO' => 'gtk-no');
             if($gnMsgDialog->run eq 'yes') {
                $gnMsgDialog->destroy;
                undef($gnMsgDialog);
                mkdir($logdir);
             }else{
                $gnMsgDialog->destroy;
                undef($gnMsgDialog);
                exit(1);
             }
         
      }

      # 画像キャッシュテーブルの初期化
      $gnInitCfg{'Main'}{'cachetbl.ini'} = $gnCfgPath . "cachetbl.ini";
      if (!(-e $gnInitCfg{'Main'}{'cachetbl.ini'})) {
         open(gnCTBL, ">$gnInitCfg{'Main'}{'cachetbl.ini'}");
         close(gnCTBL);
      }
      
      my $gnInitCfgFN = $gnCfgPath . ".gnviewconfig";
      $gnInitCfg{'Main'}{'gnCfgPath'} = $gnCfgPath;
      if($gnDebugFlag) { print Dumper(%gnInitCfg); }
      &gnSaveEnv($gnInitCfgFN, \%gnInitCfg);
      
      my $gnFirstRunGUI = $gnFRxml->get_widget('gnInitialConfig');
      $gnFirstRunGUI->destroy;
      
      &gnInit;

      #####################################################################
   
   }

}


1;

