/* -*- mode: c++; tab-width: 4; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2006-2007	Andre Beckedorf
 * 					 		<evilJazz _AT_ katastrophos _DOT_ net>
 * 
 * Copyright (C) 2005 Atmark <atmarkat _AT_ msn _DOT_ com>
 *                    AGAWA Koji <i _AT_ atty _DOT_ jp>
 *
 * 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.
 */

#ifndef QUASAR_H_INCLUDED
#define QUASAR_H_INCLUDED

#include <qmainwindow.h>
#include <qstring.h>
#include <qaction.h>
#ifdef QTOPIA
#include <qpe/qpestyle.h>
#endif

#include "playlistmanager.h"
#include "skinmanager.h"

class MPlayer;
class QWidgetStack;
class Configuration;
class MediaDatabase;
class PlayList;
class PlayListOverview;
class OperationPanel;
class ToolPanel;
class PlayListToolPanel;
class PlayInfo;
class VideoPlaybackWindow;
class SkinManager;
class QVBoxLayout;
class QVBox;
class QMenuBar;
class QToolBar;
class QCloseEvent;
class QResizeEvent;
class QKeyEvent;
class QLineEdit;
class QSplitter;

#define MinSliderLength 22

#ifdef QTOPIA
// MOC_SKIP_BEGIN
class QuasarStyle: public QPEStyle
{
public:
	QuasarStyle(): QPEStyle() {}
	~QuasarStyle() {}
	int sliderLength () const { return MinSliderLength; };
	void scrollBarMetrics(const QScrollBar *scrollbar, int &sliderMin, int &sliderMax, int &sliderLength, int &buttonDim)
	{
		QPEStyle::scrollBarMetrics(scrollbar, sliderMin, sliderMax, sliderLength, buttonDim);
		if (sliderLength < MinSliderLength)
		{
			sliderMax = sliderMax - (MinSliderLength - sliderLength); 
			sliderLength = MinSliderLength;			
		}
	}
};
// MOC_SKIP_END
#endif

class Quasar: public QMainWindow
{
	Q_OBJECT
	
signals:
	void mplayerQuit();
	void playbackStarted();
	void playbackStopped();
	
public:
	Quasar();
	~Quasar();

	enum View {
		ViewDynPlaylistEditor = 0,
		ViewOpenedPlaylist = 1,
		ViewOnTheGoPlaylist = 2,
		ViewLastActivePlaylist = 3,
		ViewPlayInfo = 4,
		ViewMax = 5
	};	
	
	static Configuration config;
	
	View activeView();
	bool setActiveView(View view);

public slots:
	// playback control
	void play_pause();
	void stop_play();
	void stop();
	void next();
	void previous();
	void setPlayOrder(int);
	void setOverviewVisibility(bool visible);
	void setInputMode(PlayList::InputMode inputMode);
	void setFullScreen(int);
	
private slots:
	void init();
	
	void play_pause(bool forcePlayNewFile);
	
	// Configuration dialog
	void configuration();
	
	// サスペンド制御
	void disableSuspend();
	void enableSuspend();
	
	void turnOnLCD();
	void turnOffLCD();
	
	void toggleFullScreen();
	void toggleInfoWidget();
	void togglePlayOrder();
	void toggleRandomPlayOrder();
	void toggleOverviewVisibility();
	void toggleSelectMode() { setInputMode(PlayList::Select); }
	void toggleMultiSelectMode() { setInputMode(PlayList::MultiSelect); }
	void toggleMoveItemMode() { setInputMode(PlayList::Move); }
	void about();
	
	void setVolumeRelative(int);
	void setVolumeAbsolute(int);
	
	void setAbsoluteSeconds(int);
	
	void setDocument(const QString&);
	
	void mplayerCurrentPlayTime(int seconds);
	void mplayerErrorMessage(QString);
	
	void resetFilterAndSorting();
	
	void filterEditBoxReturnPressed();
	void filterEditBoxTextChanged(const QString&);
	void filterEditBoxTimedUpdate();

	void setFilter(const QString&);
	void resetFilter();
	void resetFilterAndFocusFilterEditBox();
	
	void updateButtonAndMenuState();
	void scheduleUpdateButtonAndMenuState();

	void fileMenuAboutToShow();
	void editMenuAboutToShow();
	void viewMenuAboutToShow();
	void toolsMenuAboutToShow();
	
	void switchPlaylistEditorView();
	void switchToLastActivePlayList();
	void switchToCurrentPlayList();
	void switchToOnTheGoPlayList();
	void switchToInfoWidget();
	
	void playlistViewUpdated();
	void playlistViewSorted();
	void playlistManagerSwitchingModeTo(PlayListManager::Mode mode);
	void playlistManagerSwitchedModeTo(PlayListManager::Mode mode);
	void playlistToolPanelModeChanged(PlayListManager::Mode mode);	
	void playlistManagerPlayListFileNameChanged();
	void playlistManagerPlayListCleared();
	void playlistManagerPlayListFilterChanged(const QString &filter);
	
	void playlistOverviewFocusOutLeft();
	void playlistOverviewFocusOutRight();
	void setFocusPlayListOverview();
	
	void incDisplaySize();
	void decDisplaySize();
	
	void loadPrimarySkin();
	void loadSecondarySkin();
	
protected:
	void closeEvent(QCloseEvent*);
	void resizeEvent(QResizeEvent *);

	bool eventFilter(QObject *, QEvent *);

private:
	int fullScreenMenuItemID_;
	enum WIDGETID {
		InfoWidget, PlayListWidget
	};

	bool filterActive();
	
	// メニューバーのセット
	void setupMenu();

	void setupActions();
	
	void setPlayListFont(SkinManager::InfoMode mode);
	void setRemoteCon(bool shuttingDown = false);
	void changeOrientation(bool landscape, bool setSkinStyle = true, bool saveCurrentColumnWidths = true);
	void checkOrientation();
	
	void configuration(bool showMPlayerConfig);	

	void startPlaying();
	void stopPlaying(bool temporarily = false, bool stopBackend = true);
	
	MPlayer *mplayer_;
	SkinManager *skinManager_;
	
	int keyEventCount_;
	
	bool isFullScreen_;
	
	// Widgets
	QToolBar *toolBar_;
	QToolBar *subBar_;
	
	int idFileMenuNewPlaylist_;
	int idFileMenuNewDynPlaylist_;
	int idFileMenuOpenPlaylist_;
	int idFileMenuSavePlaylist_;
	int idFileMenuSaveAsPlaylist_;
	
	int idEditMenuCutItem_;
	int idEditMenuCopyItem_;
	int idEditMenuPasteItem_;
	int idEditMenuDeleteItem_;
	int idEditMenuSelectModeItem_;
	int idEditMenuMultiSelectModeItem_;
	int idEditMenuMoveItemModeItem_;
	
	int idViewMenuFullscreenItem_;
	int idViewMenuShowOverviewItem_;
	int idViewMenuCurrentPlayListItem_;
	int idViewMenuOnTheGoPlayListItem_;
	int idViewMenuDynamicPlayListEditorItem_;
	int idViewMenuPlayInfoItem_;
	int idViewMenuIncDisplaySize_;
	int idViewMenuDecDisplaySize_;
	int idViewMenuQuickswitchSkin1_;
	int idViewMenuQuickswitchSkin2_;
	
	int idToolsMenuRescanDynamicPlaylist_;
	
	QAction *acPlayInfoToggle;
	QAction *acMoveBack;
	QAction *acMoveForward;
	QAction *acDelete;
	
	PlayListManager *playListManager_;
	PlayList *playList_;
	PlayListOverview *playListOverview_;

	QString prevFilterString_;
	
	QLineEdit *filterEditBox_;
	QString newFilterText_;
	QTimer *filterEditTimer_;
	VideoPlaybackWindow *videoPlaybackWindow_;
	PlayInfo *playInfo_;
	OperationPanel *operationPanel_;
	ToolPanel *toolPanel_;
	PlayListToolPanel *playListToolPanel_;
	QTimer *updateButtonAndMenuTimer_;
	
	bool nextMediaPrefetched_;
	int currentMediaLength_;
	
	// Layout
	int currentWidth_;
	QVBoxLayout *screen_;	// Screen layout
	QWidgetStack *widgetStack_, *playlistStack_;
	QVBox *playListContainer_;
	QSplitter *splitterContainer_;
	
	// Menu
	QMenuBar *menuBar_;
	QPopupMenu *fileMenu_, *playbackOrderMenu_, *playback_, *sortByMenu_, *editMenu_, *viewMenu_, *toolsMenu_;
};

#endif // QUASAR_H_INCLUDED
