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
=== modified file 'src/Services/Encoding.vala'
--- src/Services/Encoding.vala 2013-05-01 18:28:34 +0000
+++ src/Services/Encoding.vala 2014-03-14 01:43:42 +0000
@@ -1,6 +1,6 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/***2/***
3 BEGIN LICENSE 3 BEGIN LICENSE
44
5 Copyright (C) 2012-2013 Mario Guerriero <mario@elementaryos.org>5 Copyright (C) 2012-2013 Mario Guerriero <mario@elementaryos.org>
6 This program is free software: you can redistribute it and/or modify it6 This program is free software: you can redistribute it and/or modify it
@@ -241,13 +241,12 @@
241 { EncodingType.WINDOWS_1258,241 { EncodingType.WINDOWS_1258,
242 "WINDOWS-1258", "Vietnamese" }242 "WINDOWS-1258", "Vietnamese" }
243 };243 };
244 244
245 private static bool test (string text, string charset) {245 private static bool test (string text, string charset) {
246 bool valid = false;246 bool valid = false;
247247
248 try {248 try {
249 string convert;249 string convert;
250
251 convert = GLib.convert (text, -1, "UTF-8", charset);250 convert = GLib.convert (text, -1, "UTF-8", charset);
252 valid = true;251 valid = true;
253 }252 }
@@ -256,7 +255,7 @@
256 }255 }
257 return valid;256 return valid;
258 }257 }
259 258
260 public static string get_charset (string path) {259 public static string get_charset (string path) {
261 // Get correct encoding via chardect.py script260 // Get correct encoding via chardect.py script
262261
@@ -278,22 +277,35 @@
278 }277 }
279 return charset;278 return charset;
280 }279 }
281 280
282 public string? file_content_to_utf8 (File file, string content, string mode = "r" /* it means read or write */) {281 public string? file_content_to_utf8 (File file, string content, string mode = "r" /* it means read or write */) {
283 282
284 string? encoding = null;283 string? encoding = null;
285 string? encoded_content = null;284 string? encoded_content = null;
286 285
287 encoding = get_charset (file.get_path ());286 encoding = get_charset (file.get_path ());
288 287
289 try {288 try {
290 encoded_content = GLib.convert (content, -1, "UTF-8", encoding);289 InputStream @is = file.read ();
290 CharsetConverter iconverter = new CharsetConverter ("utf-8", encoding.down ());
291 ConverterInputStream @converted = new ConverterInputStream (@is, iconverter);
292 DataInputStream dis = new DataInputStream (@converted);
293 string line = dis.read_line ();
294 string str = line;
295 while ((line = dis.read_line (null)) != null) {
296 str += line + "\n";
297 }
298 encoded_content = str;
291 } catch (GLib.ConvertError ce) {299 } catch (GLib.ConvertError ce) {
292 warning (ce.message);300 warning (ce.message);
301 } catch (IOError e) {
302 warning (e.message);
303 } catch (Error e) {
304 warning (e.message);
293 }305 }
294 306
295 return encoded_content;307 return encoded_content;
296 308
297 }309 }
298310
299}311}

Subscribers

People subscribed via source and target branches