|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 #include "nsDeviceContextAndroid.h" |
|
5 #include "nsString.h" |
|
6 #include "nsIFile.h" |
|
7 #include "nsIFileStreams.h" |
|
8 #include "nsAutoPtr.h" |
|
9 #include "gfxPDFSurface.h" |
|
10 #include "nsIPrintSettings.h" |
|
11 #include "nsDirectoryServiceDefs.h" |
|
12 |
|
13 NS_IMPL_ISUPPORTS(nsDeviceContextSpecAndroid, nsIDeviceContextSpec) |
|
14 |
|
15 NS_IMETHODIMP |
|
16 nsDeviceContextSpecAndroid::GetSurfaceForPrinter(gfxASurface** aSurface) |
|
17 { |
|
18 nsresult rv = |
|
19 NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mTempFile)); |
|
20 NS_ENSURE_SUCCESS(rv, rv); |
|
21 |
|
22 nsAutoCString filename("tmp-printing.pdf"); |
|
23 mTempFile->AppendNative(filename); |
|
24 rv = mTempFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0660); |
|
25 NS_ENSURE_SUCCESS(rv, rv); |
|
26 |
|
27 nsCOMPtr<nsIFileOutputStream> stream = do_CreateInstance("@mozilla.org/network/file-output-stream;1"); |
|
28 rv = stream->Init(mTempFile, -1, -1, 0); |
|
29 NS_ENSURE_SUCCESS(rv, rv); |
|
30 |
|
31 nsRefPtr<gfxASurface> surface; |
|
32 |
|
33 // XXX: what should we do here for size? screen size? |
|
34 gfxSize surfaceSize(480, 800); |
|
35 |
|
36 surface = new gfxPDFSurface(stream, surfaceSize); |
|
37 |
|
38 |
|
39 NS_ABORT_IF_FALSE(surface, "valid address expected"); |
|
40 surface.swap(*aSurface); |
|
41 return NS_OK; |
|
42 } |
|
43 |
|
44 NS_IMETHODIMP |
|
45 nsDeviceContextSpecAndroid::Init(nsIWidget* aWidget, |
|
46 nsIPrintSettings* aPS, |
|
47 bool aIsPrintPreview) |
|
48 { |
|
49 mPrintSettings = aPS; |
|
50 return NS_OK; |
|
51 } |
|
52 |
|
53 NS_IMETHODIMP |
|
54 nsDeviceContextSpecAndroid::BeginDocument(const nsAString& aTitle, |
|
55 char16_t* aPrintToFileName, |
|
56 int32_t aStartPage, |
|
57 int32_t aEndPage) |
|
58 { |
|
59 return NS_OK; |
|
60 } |
|
61 |
|
62 NS_IMETHODIMP |
|
63 nsDeviceContextSpecAndroid::EndDocument() |
|
64 { |
|
65 nsXPIDLString targetPath; |
|
66 nsCOMPtr<nsIFile> destFile; |
|
67 mPrintSettings->GetToFileName(getter_Copies(targetPath)); |
|
68 |
|
69 nsresult rv = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(targetPath), |
|
70 false, getter_AddRefs(destFile)); |
|
71 NS_ENSURE_SUCCESS(rv, rv); |
|
72 |
|
73 nsAutoString destLeafName; |
|
74 rv = destFile->GetLeafName(destLeafName); |
|
75 NS_ENSURE_SUCCESS(rv, rv); |
|
76 |
|
77 nsCOMPtr<nsIFile> destDir; |
|
78 rv = destFile->GetParent(getter_AddRefs(destDir)); |
|
79 NS_ENSURE_SUCCESS(rv, rv); |
|
80 |
|
81 rv = mTempFile->MoveTo(destDir, destLeafName); |
|
82 NS_ENSURE_SUCCESS(rv, rv); |
|
83 |
|
84 destFile->SetPermissions(0666); |
|
85 return NS_OK; |
|
86 } |
|
87 |
|
88 NS_IMETHODIMP |
|
89 nsDeviceContextSpecAndroid::GetPath (const char** aPath) |
|
90 { |
|
91 return NS_OK; |
|
92 } |