Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: chrome/browser/ui/webui/print_preview_handler.cc

Issue 6759044: Prepopulating "Print To PDF" select file dialog with a suggested filename and path. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remembering last used path. Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/print_preview_handler.h" 5 #include "chrome/browser/ui/webui/print_preview_handler.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/i18n/file_util_icu.h"
9 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/path_service.h"
10 #include "base/threading/thread.h" 12 #include "base/threading/thread.h"
11 #include "base/values.h" 13 #include "base/values.h"
12 #include "chrome/browser/platform_util.h" 14 #include "chrome/browser/platform_util.h"
13 #include "chrome/browser/printing/print_preview_tab_controller.h" 15 #include "chrome/browser/printing/print_preview_tab_controller.h"
14 #include "chrome/browser/ui/webui/print_preview_ui_html_source.h" 16 #include "chrome/browser/ui/webui/print_preview_ui_html_source.h"
15 #include "chrome/browser/ui/webui/print_preview_ui.h" 17 #include "chrome/browser/ui/webui/print_preview_ui.h"
18 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/print_messages.h" 19 #include "chrome/common/print_messages.h"
17 #include "content/browser/browser_thread.h" 20 #include "content/browser/browser_thread.h"
18 #include "content/browser/renderer_host/render_view_host.h" 21 #include "content/browser/renderer_host/render_view_host.h"
19 #include "content/browser/tab_contents/tab_contents.h" 22 #include "content/browser/tab_contents/tab_contents.h"
23 #include "grit/generated_resources.h"
20 #include "printing/backend/print_backend.h" 24 #include "printing/backend/print_backend.h"
21 #include "printing/metafile.h" 25 #include "printing/metafile.h"
22 #include "printing/metafile_impl.h" 26 #include "printing/metafile_impl.h"
23 #include "printing/print_job_constants.h" 27 #include "printing/print_job_constants.h"
28 #include "ui/base/l10n/l10n_util.h"
24 29
25 namespace { 30 namespace {
26 31
27 const bool kColorDefaultValue = false; 32 const bool kColorDefaultValue = false;
28 const bool kLandscapeDefaultValue = false; 33 const bool kLandscapeDefaultValue = false;
29 34
30 TabContents* GetInitiatorTab(TabContents* preview_tab) { 35 TabContents* GetInitiatorTab(TabContents* preview_tab) {
31 printing::PrintPreviewTabController* tab_controller = 36 printing::PrintPreviewTabController* tab_controller =
32 printing::PrintPreviewTabController::GetInstance(); 37 printing::PrintPreviewTabController::GetInstance();
33 if (!tab_controller) 38 if (!tab_controller)
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 } 138 }
134 139
135 private: 140 private:
136 // The metafile holding the PDF data. 141 // The metafile holding the PDF data.
137 scoped_ptr<printing::Metafile> metafile_; 142 scoped_ptr<printing::Metafile> metafile_;
138 143
139 // The absolute path where the file will be saved. 144 // The absolute path where the file will be saved.
140 FilePath path_; 145 FilePath path_;
141 }; 146 };
142 147
148 FilePath* PrintPreviewHandler::last_saved_path_ = NULL;
149
143 PrintPreviewHandler::PrintPreviewHandler() 150 PrintPreviewHandler::PrintPreviewHandler()
144 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), 151 : print_backend_(printing::PrintBackend::CreateInstance(NULL)),
145 need_to_generate_preview_(true), 152 need_to_generate_preview_(true),
146 color_(kColorDefaultValue), 153 color_(kColorDefaultValue),
147 landscape_(kLandscapeDefaultValue) { 154 landscape_(kLandscapeDefaultValue) {
155 if (!last_saved_path_) {
156 last_saved_path_ = new FilePath();
157 PathService::Get(chrome::DIR_USER_DOCUMENTS, last_saved_path_);
Lei Zhang 2011/04/14 00:15:45 BTW, you can't do this - file access on the UI thr
dpapad 2011/04/14 02:33:35 Added a scoped exception for the moment. Alternati
158 }
148 } 159 }
149 160
150 PrintPreviewHandler::~PrintPreviewHandler() { 161 PrintPreviewHandler::~PrintPreviewHandler() {
151 if (select_file_dialog_.get()) 162 if (select_file_dialog_.get())
152 select_file_dialog_->ListenerDestroyed(); 163 select_file_dialog_->ListenerDestroyed();
153 } 164 }
154 165
155 void PrintPreviewHandler::RegisterMessages() { 166 void PrintPreviewHandler::RegisterMessages() {
156 web_ui_->RegisterMessageCallback("getPrinters", 167 web_ui_->RegisterMessageCallback("getPrinters",
157 NewCallback(this, &PrintPreviewHandler::HandleGetPrinters)); 168 NewCallback(this, &PrintPreviewHandler::HandleGetPrinters));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); 206 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents());
196 if (initiator_tab) { 207 if (initiator_tab) {
197 RenderViewHost* rvh = initiator_tab->render_view_host(); 208 RenderViewHost* rvh = initiator_tab->render_view_host();
198 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); 209 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id()));
199 } 210 }
200 211
201 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); 212 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args));
202 if (!settings.get()) 213 if (!settings.get())
203 return; 214 return;
204 215
205 bool print_to_pdf; 216 bool print_to_pdf = false;
206 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); 217 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf);
207 218
208 if (print_to_pdf) { 219 if (print_to_pdf) {
209 SelectFile(); 220 // Pre-populating select file dialog with print job title.
221 FilePath::StringType print_job_title;
Lei Zhang 2011/04/14 00:15:45 I think you can instead just do the following:
dpapad 2011/04/14 02:33:35 Done.
222 bool ret = settings->GetString(printing::kPrintJobTitle, &print_job_title);
223 if (!ret || print_job_title.length() == 0)
224 print_job_title = l10n_util::GetStringUTF8(
225 IDS_DEFAULT_PRINT_DOCUMENT_TITLE);
226
227 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_');
228 FilePath default_filename(print_job_title);
229 default_filename =
230 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf"));
231
232 SelectFile(default_filename);
210 } else { 233 } else {
211 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); 234 RenderViewHost* rvh = web_ui_->GetRenderViewHost();
212 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); 235 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings));
213 } 236 }
214 } 237 }
215 238
216 void PrintPreviewHandler::SendPrinterList( 239 void PrintPreviewHandler::SendPrinterList(
217 const ListValue& printers, 240 const ListValue& printers,
218 const FundamentalValue& default_printer_index) { 241 const FundamentalValue& default_printer_index) {
219 web_ui_->CallJavascriptFunction("setPrinters", printers, 242 web_ui_->CallJavascriptFunction("setPrinters", printers,
(...skipping 13 matching lines...) Expand all
233 void PrintPreviewHandler::ProcessLandscapeSetting( 256 void PrintPreviewHandler::ProcessLandscapeSetting(
234 const DictionaryValue& settings) { 257 const DictionaryValue& settings) {
235 bool landscape = kLandscapeDefaultValue; 258 bool landscape = kLandscapeDefaultValue;
236 settings.GetBoolean(printing::kSettingLandscape, &landscape); 259 settings.GetBoolean(printing::kSettingLandscape, &landscape);
237 if (landscape != landscape_) { 260 if (landscape != landscape_) {
238 landscape_ = landscape; 261 landscape_ = landscape;
239 need_to_generate_preview_ = true; 262 need_to_generate_preview_ = true;
240 } 263 }
241 } 264 }
242 265
243 void PrintPreviewHandler::SelectFile() { 266 void PrintPreviewHandler::SelectFile(const FilePath& default_filename) {
244 SelectFileDialog::FileTypeInfo file_type_info; 267 SelectFileDialog::FileTypeInfo file_type_info;
245 file_type_info.extensions.resize(1); 268 file_type_info.extensions.resize(1);
246 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); 269 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf"));
247 270
248 if (!select_file_dialog_.get()) 271 if (!select_file_dialog_.get())
249 select_file_dialog_ = SelectFileDialog::Create(this); 272 select_file_dialog_ = SelectFileDialog::Create(this);
250 273
251 select_file_dialog_->SelectFile( 274 select_file_dialog_->SelectFile(
252 SelectFileDialog::SELECT_SAVEAS_FILE, 275 SelectFileDialog::SELECT_SAVEAS_FILE,
253 string16(), 276 string16(),
254 FilePath(), 277 last_saved_path_->Append(default_filename),
255 &file_type_info, 278 &file_type_info,
256 0, 279 0,
257 FILE_PATH_LITERAL(""), 280 FILE_PATH_LITERAL(""),
258 platform_util::GetTopLevel( 281 platform_util::GetTopLevel(
259 web_ui_->tab_contents()->GetNativeView()), 282 web_ui_->tab_contents()->GetNativeView()),
260 NULL); 283 NULL);
261 } 284 }
262 285
263 void PrintPreviewHandler::FileSelected(const FilePath& path, 286 void PrintPreviewHandler::FileSelected(const FilePath& path,
264 int index, void* params) { 287 int index, void* params) {
265 PrintPreviewUIHTMLSource::PrintPreviewData data; 288 PrintPreviewUIHTMLSource::PrintPreviewData data;
266 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); 289 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_);
267 pp_ui->html_source()->GetPrintPreviewData(&data); 290 pp_ui->html_source()->GetPrintPreviewData(&data);
268 DCHECK(data.first != NULL); 291 DCHECK(data.first != NULL);
269 DCHECK(data.second > 0); 292 DCHECK(data.second > 0);
270 293
271 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; 294 printing::PreviewMetafile* metafile = new printing::PreviewMetafile;
272 metafile->InitFromData(data.first->memory(), data.second); 295 metafile->InitFromData(data.first->memory(), data.second);
273 296
297 // Updating last_saved_path_ to the newly selected folder.
298 *last_saved_path_ = path.DirName();
299
274 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); 300 PrintToPdfTask* task = new PrintToPdfTask(metafile, path);
275 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); 301 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task);
276 } 302 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698