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

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: Getting print job title in a different way. 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/native_metafile_factory.h" 25 #include "printing/native_metafile_factory.h"
22 #include "printing/native_metafile.h" 26 #include "printing/native_metafile.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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); 200 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents());
196 if (initiator_tab) { 201 if (initiator_tab) {
197 RenderViewHost* rvh = initiator_tab->render_view_host(); 202 RenderViewHost* rvh = initiator_tab->render_view_host();
198 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); 203 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id()));
199 } 204 }
200 205
201 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); 206 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args));
202 if (!settings.get()) 207 if (!settings.get())
203 return; 208 return;
204 209
205 bool print_to_pdf; 210 bool print_to_pdf = false;
206 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); 211 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf);
207 212
208 if (print_to_pdf) { 213 if (print_to_pdf) {
209 SelectFile(); 214 // Pre-populating select file dialog with print job title.
215 FilePath::StringType print_job_title;
216 bool ret = settings->GetString(printing::kPrintJobTitle, &print_job_title);
217 if (!ret || print_job_title.length() == 0)
kmadhusu 2011/04/11 17:34:26 If "ret" value is false, we need to return. Please
dpapad 2011/04/12 00:20:09 Why do we want to prevent the user from printing,
218 print_job_title = l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE);
kmadhusu 2011/04/11 17:34:26 We don't want to display the default file name as
dpapad 2011/04/12 00:20:09 Done.
219
220 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_');
221 FilePath default_path;
222 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path);
223 FilePath default_filename(print_job_title);
224 default_filename =
225 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf"));
226
227 SelectFile(default_path.Append(default_filename));
210 } else { 228 } else {
211 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); 229 RenderViewHost* rvh = web_ui_->GetRenderViewHost();
212 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); 230 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings));
213 } 231 }
214 } 232 }
215 233
216 void PrintPreviewHandler::SendPrinterList( 234 void PrintPreviewHandler::SendPrinterList(
217 const ListValue& printers, 235 const ListValue& printers,
218 const FundamentalValue& default_printer_index) { 236 const FundamentalValue& default_printer_index) {
219 web_ui_->CallJavascriptFunction("setPrinters", printers, 237 web_ui_->CallJavascriptFunction("setPrinters", printers,
(...skipping 13 matching lines...) Expand all
233 void PrintPreviewHandler::ProcessLandscapeSetting( 251 void PrintPreviewHandler::ProcessLandscapeSetting(
234 const DictionaryValue& settings) { 252 const DictionaryValue& settings) {
235 bool landscape = kLandscapeDefaultValue; 253 bool landscape = kLandscapeDefaultValue;
236 settings.GetBoolean(printing::kSettingLandscape, &landscape); 254 settings.GetBoolean(printing::kSettingLandscape, &landscape);
237 if (landscape != landscape_) { 255 if (landscape != landscape_) {
238 landscape_ = landscape; 256 landscape_ = landscape;
239 need_to_generate_preview_ = true; 257 need_to_generate_preview_ = true;
240 } 258 }
241 } 259 }
242 260
243 void PrintPreviewHandler::SelectFile() { 261 void PrintPreviewHandler::SelectFile(const FilePath& default_path) {
244 SelectFileDialog::FileTypeInfo file_type_info; 262 SelectFileDialog::FileTypeInfo file_type_info;
245 file_type_info.extensions.resize(1); 263 file_type_info.extensions.resize(1);
246 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); 264 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf"));
247 265
248 if (!select_file_dialog_.get()) 266 if (!select_file_dialog_.get())
249 select_file_dialog_ = SelectFileDialog::Create(this); 267 select_file_dialog_ = SelectFileDialog::Create(this);
250 268
251 select_file_dialog_->SelectFile( 269 select_file_dialog_->SelectFile(
252 SelectFileDialog::SELECT_SAVEAS_FILE, 270 SelectFileDialog::SELECT_SAVEAS_FILE,
253 string16(), 271 string16(),
254 FilePath(), 272 default_path,
255 &file_type_info, 273 &file_type_info,
256 0, 274 0,
257 FILE_PATH_LITERAL(""), 275 FILE_PATH_LITERAL(""),
258 platform_util::GetTopLevel( 276 platform_util::GetTopLevel(
259 web_ui_->tab_contents()->GetNativeView()), 277 web_ui_->tab_contents()->GetNativeView()),
260 NULL); 278 NULL);
261 } 279 }
262 280
263 void PrintPreviewHandler::FileSelected(const FilePath& path, 281 void PrintPreviewHandler::FileSelected(const FilePath& path,
264 int index, void* params) { 282 int index, void* params) {
265 #if defined(OS_POSIX) 283 #if defined(OS_POSIX)
266 PrintPreviewUIHTMLSource::PrintPreviewData data; 284 PrintPreviewUIHTMLSource::PrintPreviewData data;
267 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); 285 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_);
268 pp_ui->html_source()->GetPrintPreviewData(&data); 286 pp_ui->html_source()->GetPrintPreviewData(&data);
269 DCHECK(data.first != NULL); 287 DCHECK(data.first != NULL);
270 DCHECK(data.second > 0); 288 DCHECK(data.second > 0);
271 289
272 printing::NativeMetafile* metafile = 290 printing::NativeMetafile* metafile =
273 printing::NativeMetafileFactory::CreateFromData(data.first->memory(), 291 printing::NativeMetafileFactory::CreateFromData(data.first->memory(),
274 data.second); 292 data.second);
275 metafile->FinishDocument(); 293 metafile->FinishDocument();
276 294
277 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); 295 PrintToPdfTask* task = new PrintToPdfTask(metafile, path);
278 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); 296 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task);
279 #else 297 #else
280 NOTIMPLEMENTED(); 298 NOTIMPLEMENTED();
281 #endif 299 #endif
282 } 300 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698