Home | History | Annotate | Download | only in download
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_GTK_DOWNLOAD_DOWNLOAD_ITEM_GTK_H_
      6 #define CHROME_BROWSER_UI_GTK_DOWNLOAD_DOWNLOAD_ITEM_GTK_H_
      7 #pragma once
      8 
      9 #include <gtk/gtk.h>
     10 
     11 #include <string>
     12 
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/time.h"
     15 #include "chrome/browser/download/download_item.h"
     16 #include "chrome/browser/icon_manager.h"
     17 #include "chrome/browser/ui/gtk/owned_widget_gtk.h"
     18 #include "content/common/notification_observer.h"
     19 #include "content/common/notification_registrar.h"
     20 #include "ui/base/animation/animation_delegate.h"
     21 #include "ui/base/animation/slide_animation.h"
     22 #include "ui/base/gtk/gtk_signal.h"
     23 
     24 class BaseDownloadItemModel;
     25 class DownloadShelfContextMenuGtk;
     26 class DownloadShelfGtk;
     27 class GtkThemeService;
     28 class NineBox;
     29 class SkBitmap;
     30 
     31 namespace gfx {
     32 class Image;
     33 }
     34 
     35 namespace ui {
     36 class SlideAnimation;
     37 }
     38 
     39 class DownloadItemGtk : public DownloadItem::Observer,
     40                         public ui::AnimationDelegate,
     41                         public NotificationObserver {
     42  public:
     43   // DownloadItemGtk takes ownership of |download_item_model|.
     44   DownloadItemGtk(DownloadShelfGtk* parent_shelf,
     45                   BaseDownloadItemModel* download_item_model);
     46 
     47   // Destroys all widgets belonging to this DownloadItemGtk.
     48   ~DownloadItemGtk();
     49 
     50   // DownloadItem::Observer implementation.
     51   virtual void OnDownloadUpdated(DownloadItem* download);
     52   virtual void OnDownloadOpened(DownloadItem* download) { }
     53 
     54   // ui::AnimationDelegate implementation.
     55   virtual void AnimationProgressed(const ui::Animation* animation);
     56 
     57   // Overridden from NotificationObserver:
     58   virtual void Observe(NotificationType type,
     59                        const NotificationSource& source,
     60                        const NotificationDetails& details);
     61 
     62   // Called when the icon manager has finished loading the icon. We take
     63   // ownership of |icon_bitmap|.
     64   void OnLoadSmallIconComplete(IconManager::Handle handle,
     65                                gfx::Image* image);
     66   void OnLoadLargeIconComplete(IconManager::Handle handle,
     67                                gfx::Image* image);
     68 
     69   // Returns the DownloadItem model object belonging to this item.
     70   DownloadItem* get_download();
     71 
     72  private:
     73   friend class DownloadShelfContextMenuGtk;
     74 
     75   // Returns true IFF the download is dangerous and unconfirmed.
     76   bool IsDangerous();
     77 
     78   // Functions for controlling the progress animation.
     79   // Repaint the download progress.
     80   void UpdateDownloadProgress();
     81 
     82   // Starts a repeating timer for UpdateDownloadProgress.
     83   void StartDownloadProgress();
     84 
     85   // Stops the repeating timer.
     86   void StopDownloadProgress();
     87 
     88   // Ask the icon manager to asynchronously start loading the icon for the file.
     89   void LoadIcon();
     90 
     91   // Sets the tooltip on the download button.
     92   void UpdateTooltip();
     93 
     94   // Sets the name label to the correct color.
     95   void UpdateNameLabel();
     96 
     97   // Sets the text of |status_label_| with the correct color.
     98   void UpdateStatusLabel(const std::string& status_text);
     99 
    100   // Sets the components of the danger warning.
    101   void UpdateDangerWarning();
    102 
    103   // Sets the icon for the danger warning dialog.
    104   void UpdateDangerIcon();
    105 
    106   static void InitNineBoxes();
    107 
    108   // Show popup context menu. If |button| is not NULL, show the menu dropping
    109   // down from |button|. Otherwise, show the menu where the user clicks.
    110   void ShowPopupMenu(GtkWidget* button, GdkEventButton* event);
    111 
    112   // Draws everything in GTK rendering mode.
    113   CHROMEGTK_CALLBACK_1(DownloadItemGtk, gboolean, OnHboxExpose,
    114                        GdkEventExpose*);
    115 
    116   // Used for the download item's body and menu button in chrome theme mode.
    117   CHROMEGTK_CALLBACK_1(DownloadItemGtk, gboolean, OnExpose, GdkEventExpose*);
    118 
    119   // Called when |body_| is clicked.
    120   CHROMEGTK_CALLBACK_0(DownloadItemGtk, void, OnClick);
    121 
    122   // Called when |body_| is pressed with mouse button. This function is used to
    123   // show popup menu with right button click.
    124   CHROMEGTK_CALLBACK_1(DownloadItemGtk, gboolean, OnButtonPress,
    125                        GdkEventButton*);
    126 
    127   // Used for the download icon.
    128   CHROMEGTK_CALLBACK_1(DownloadItemGtk, gboolean, OnProgressAreaExpose,
    129                        GdkEventExpose*);
    130 
    131   CHROMEGTK_CALLBACK_1(DownloadItemGtk, gboolean, OnMenuButtonPressEvent,
    132                        GdkEventButton*);
    133 
    134   // Dangerous download related. -----------------------------------------------
    135   CHROMEGTK_CALLBACK_1(DownloadItemGtk, gboolean, OnDangerousPromptExpose,
    136                        GdkEventExpose*);
    137   CHROMEGTK_CALLBACK_0(DownloadItemGtk, void, OnDangerousAccept);
    138   CHROMEGTK_CALLBACK_0(DownloadItemGtk, void, OnDangerousDecline);
    139 
    140   // Nineboxes for the body area.
    141   static NineBox* body_nine_box_normal_;
    142   static NineBox* body_nine_box_prelight_;
    143   static NineBox* body_nine_box_active_;
    144 
    145   // Nineboxes for the menu button.
    146   static NineBox* menu_nine_box_normal_;
    147   static NineBox* menu_nine_box_prelight_;
    148   static NineBox* menu_nine_box_active_;
    149 
    150   // Ninebox for the background of the dangerous download prompt.
    151   static NineBox* dangerous_nine_box_;
    152 
    153   // The shelf on which we are displayed.
    154   DownloadShelfGtk* parent_shelf_;
    155 
    156   // The widget that contains the body and menu dropdown.
    157   OwnedWidgetGtk hbox_;
    158 
    159   // The widget that contains the name of the download and the progress
    160   // animation.
    161   OwnedWidgetGtk body_;
    162 
    163   // The GtkLabel that holds the download title text.
    164   GtkWidget* name_label_;
    165 
    166   // The GtkLabel that holds the status text.
    167   GtkWidget* status_label_;
    168 
    169   // The current text of status label
    170   std::string status_text_;
    171 
    172   // The widget that creates a dropdown menu when pressed.
    173   GtkWidget* menu_button_;
    174 
    175   // A gtk arrow pointing downward displayed in |menu_button_|. Only displayed
    176   // in GTK mode.
    177   GtkWidget* arrow_;
    178 
    179   // Whether the menu is currently showing for |menu_button_|. Affects how we
    180   // draw the button.
    181   bool menu_showing_;
    182 
    183   // Whether we should use the GTK text color
    184   GtkThemeService* theme_service_;
    185 
    186   // The widget that contains the animation progress and the file's icon
    187   // (as well as the complete animation).
    188   OwnedWidgetGtk progress_area_;
    189 
    190   // In degrees. Only used for downloads with no known total size.
    191   int progress_angle_;
    192 
    193   // The menu that pops down when the user presses |menu_button_|. We do not
    194   // create this until the first time we actually need it.
    195   scoped_ptr<DownloadShelfContextMenuGtk> menu_;
    196 
    197   // The download item model we represent.
    198   scoped_ptr<BaseDownloadItemModel> download_model_;
    199 
    200   // The dangerous download dialog. This will be null for safe downloads.
    201   GtkWidget* dangerous_prompt_;
    202   GtkWidget* dangerous_image_;
    203   GtkWidget* dangerous_label_;
    204 
    205   // An hbox for holding components of the dangerous download dialog.
    206   OwnedWidgetGtk dangerous_hbox_;
    207   int dangerous_hbox_start_width_;
    208   int dangerous_hbox_full_width_;
    209 
    210   // The animation when this item is first added to the shelf.
    211   scoped_ptr<ui::SlideAnimation> new_item_animation_;
    212 
    213   // Progress animation.
    214   base::RepeatingTimer<DownloadItemGtk> progress_timer_;
    215 
    216   // Animation for download complete.
    217   ui::SlideAnimation complete_animation_;
    218 
    219   // The file icon for the download. May be null. The small version is used
    220   // for display in the shelf; the large version is for use as a drag icon.
    221   // These icons are owned by the IconManager (owned by the BrowserProcess).
    222   gfx::Image* icon_small_;
    223   gfx::Image* icon_large_;
    224 
    225   // The last download file path for which we requested an icon.
    226   FilePath icon_filepath_;
    227 
    228   NotificationRegistrar registrar_;
    229 
    230   // The time at which we were insantiated.
    231   base::Time creation_time_;
    232 
    233   // For canceling an in progress icon request.
    234   CancelableRequestConsumerT<int, 0> icon_consumer_;
    235 
    236   // Indicates when the download has completed, so we don't redo
    237   // on-completion actions.
    238   bool download_complete_;
    239 };
    240 
    241 #endif  // CHROME_BROWSER_UI_GTK_DOWNLOAD_DOWNLOAD_ITEM_GTK_H_
    242