Merge lp:~garrettdreyfus/scratch/encode-fix into lp:~elementary-apps/scratch/scratch

Proposed by Garrett
Status: Merged
Approved by: Mario Guerriero
Approved revision: 1258
Merged at revision: 1261
Proposed branch: lp:~garrettdreyfus/scratch/encode-fix
Merge into: lp:~elementary-apps/scratch/scratch
Diff against target: 78 lines (+23/-11)
1 file modified
src/Services/Encoding.vala (+23/-11)
To merge this branch: bzr merge lp:~garrettdreyfus/scratch/encode-fix
Reviewer Review Type Date Requested Status
Mario Guerriero (community) Approve
Review via email: mp+210939@code.launchpad.net

Commit message

Use ConverterInputStream to properly read encoded files to fix bug #1114439

Description of the change

This patch should fix this bug https://bugs.launchpad.net/scratch/+bug/1114439. The only file changed is ./src/Services/Encoding.vala/ This is my first patch ever so I apologize for any mistakes.

To post a comment you must log in.
Revision history for this message
Raphael Isemann (teemperor) wrote :

Some comments on formatting the code:
Line 302 -> Needs a space behind the )

Otherwise: Use 4 spaces instead of Tabs. Look at the settings in Scratch (Gear on the upper right): There is a switch for turning a tab automatically into 4 spaces. There is also a switch for showing whitespace-characters which helps you to see where you did formatting errors. You never need to use tabs when you code with the styleguide of elementary. You can also activate the addon which automatically strips whitespace from the end of a line. That's already wrong in that sourcefile so you could also fix that in the same merge-request :)

Here is the styleguide for further reading: http://elementaryos.org/docs/code/code-style

When you corrected your branch you can just push your new commit to the branch and this merge-requests gets updated ;)

1256. By Garrett Finucane <email address hidden>

I fixed my styling I believe

Revision history for this message
Garrett (garrettdreyfus) wrote :

Thank you very much I believe I fixed the styling you mentioned.

On Thu, Mar 13, 2014 at 9:00 PM, Raphael Isemann
<email address hidden> wrote:
> Some comments on formatting the code:
> Line 302 -> Needs a space behind the )
>
> Otherwise: Use 4 spaces instead of Tabs. Look at the settings in Scratch (Gear on the upper right): There is a switch for turning a tab automatically into 4 spaces. There is also a switch for showing whitespace-characters which helps you to see where you did formatting errors. You never need to use tabs when you code with the styleguide of elementary. You can also activate the addon which automatically strips whitespace from the end of a line. That's already wrong in that sourcefile so you could also fix that in the same merge-request :)
>
> Here is the styleguide for further reading: http://elementaryos.org/docs/code/code-style
>
> When you corrected your branch you can just push your new commit to the branch and this merge-requests gets updated ;)
> --
> https://code.launchpad.net/~garrettdreyfus/scratch/encode-fix/+merge/210939
> You are the owner of lp:~garrettdreyfus/scratch/encode-fix.

Revision history for this message
Raphael Isemann (teemperor) wrote :

I missed two things:
"encoding.down()" should be "encoding.down ()" as there has to be a space before that opening-bracket.
Same goes for "new ConverterInputStream(" should be "new ConverterInputStream (".

I can't test it at the moment as i'm not on Isis (or my Luna install is broken), so you have to wait for someone else to review if your patch work as intended. I'll test otherwise tomorrow by myself :)

1257. By Garrett Finucane <email address hidden>

Spaced out the parantheses

1258. By Garrett Finucane <email address hidden>

Changed stdout.printf to warning

Revision history for this message
Mario Guerriero (mefrio-g) wrote :

you did a great job! It should fix all Scatch's encoding problems.

review: Approve
Revision history for this message
Mario Guerriero (mefrio-g) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Services/Encoding.vala'
2--- src/Services/Encoding.vala 2013-05-01 18:28:34 +0000
3+++ src/Services/Encoding.vala 2014-03-14 01:43:42 +0000
4@@ -1,6 +1,6 @@
5 // -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
6 /***
7- BEGIN LICENSE
8+ BEGIN LICENSE
9
10 Copyright (C) 2012-2013 Mario Guerriero <mario@elementaryos.org>
11 This program is free software: you can redistribute it and/or modify it
12@@ -241,13 +241,12 @@
13 { EncodingType.WINDOWS_1258,
14 "WINDOWS-1258", "Vietnamese" }
15 };
16-
17+
18 private static bool test (string text, string charset) {
19 bool valid = false;
20
21 try {
22 string convert;
23-
24 convert = GLib.convert (text, -1, "UTF-8", charset);
25 valid = true;
26 }
27@@ -256,7 +255,7 @@
28 }
29 return valid;
30 }
31-
32+
33 public static string get_charset (string path) {
34 // Get correct encoding via chardect.py script
35
36@@ -278,22 +277,35 @@
37 }
38 return charset;
39 }
40-
41+
42 public string? file_content_to_utf8 (File file, string content, string mode = "r" /* it means read or write */) {
43-
44+
45 string? encoding = null;
46 string? encoded_content = null;
47-
48+
49 encoding = get_charset (file.get_path ());
50-
51+
52 try {
53- encoded_content = GLib.convert (content, -1, "UTF-8", encoding);
54+ InputStream @is = file.read ();
55+ CharsetConverter iconverter = new CharsetConverter ("utf-8", encoding.down ());
56+ ConverterInputStream @converted = new ConverterInputStream (@is, iconverter);
57+ DataInputStream dis = new DataInputStream (@converted);
58+ string line = dis.read_line ();
59+ string str = line;
60+ while ((line = dis.read_line (null)) != null) {
61+ str += line + "\n";
62+ }
63+ encoded_content = str;
64 } catch (GLib.ConvertError ce) {
65 warning (ce.message);
66+ } catch (IOError e) {
67+ warning (e.message);
68+ } catch (Error e) {
69+ warning (e.message);
70 }
71-
72+
73 return encoded_content;
74-
75+
76 }
77
78 }

Subscribers

People subscribed via source and target branches