My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
´╗┐using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using NepaliWikiPediaTranslator.AWBot;
using WikiFunctions;

namespace NepaliWikiPediaTranslator
{
public partial class FormMainWindow
{



private StringReader PrintReader;

private bool EnableClipBoardCopyPaste { get; set; }


delegate void SetPercent(int percent); //setpercent is a delegate which will be used to communicate between the postprocessor thread to gett the percentage .. how much the work has been done..
delegate void SetTextCallback(string text); //set textcallback is used to get the translated text

delegate void SetHindiText(string message); // delegate to Change the content of the richtextbox
event SetHindiText OnHindiTextObtained; //event that is triggered when the Template Copying thread completes






private readonly WikiDiff Diff = new WikiDiff();



void SetPercentMethod(int percent) //this is the method which updates the progressbar after it gets the value from the delegate setpercent
{

toolStripProgressBar1.ProgressBar.Value = percent;
}

void LaunchHelpFile()
{
if (File.Exists(SessionsManager.HELPFILENAME))
{
try
{
System.Diagnostics.Process.Start(SessionsManager.HELPFILENAME);
}
catch (Exception)
{

//TODO: Enable logging:
//Catch exception
}

}


}

/// <summary>
/// Opens a content of a file and puts in the hindi tab ready for translating/editing
/// </summary>
/// <param name="inputFileName"></param>
public void OpenInputFile(string inputFileName)
{

string tempString;
string inputText = "";
StreamReader sr = new StreamReader(inputFileName);
while ((tempString = sr.ReadLine()) != null)
{
inputText += (tempString + "\n");
}
sr.Close();
this.richTextBoxHindi.Text = inputText;

}

/// <summary>
/// This method is used for finding text inside the main window
/// </summary>
private void FindInsideFormMainWindow()
{
if (searchBox == null || searchBox.IsDisposed)
{
searchBox = new SearchBox();

}

if (tabPageHindi.ContainsFocus)
searchBox.rtb = this.richTextBoxHindi;
else
searchBox.rtb = this.richTextBoxNepali;


//this.Hide();
searchBox.Show();
}

/// <summary>
/// Perform translation if everything is okay
/// Preconditions:
/// 1. the rules must be loaded
/// 2. Noun file, pronoun file, verbs file, adjectives file, and the rules file should be present in the application directory
/// 3. Postcondition: the above files in (2) should be editable by the application (I mean they should not be read only)
/// </summary>
public void PerformTranslationWithGuiUpdates()
{
_IsEditorBusy = true;
if(string.IsNullOrEmpty(richTextBoxHindi.Text))
{
MessageBox.Show("There is nothing in the Hindi tab to translate from Hindi to Nepali");
return;
}
//resetting the progressbar to zero
toolStripProgressBar1.ProgressBar.Value = toolStripProgressBar1.ProgressBar.Minimum;

//Loading rules before starting translating
pr = new PostProcessor(SessionsManager.RuleFileName, SessionsManager.VerbListFileName, SessionsManager.NounListFileName, SessionsManager.AdjectiveListFileName, SessionsManager.PronounListFileName, SessionsManager.UsersCustomFileName);

//Getting the source text
//pr.LoadTextToTranslate(richTextBoxHindi.Text);
pr.LoadTextToTranslate(richTextBoxHindi.Text);


pr.OnFeedback += new PostProcessor.ProgressFeedbackDelgate(C_OnFeedBack); //this event is required for updating the progressbar
pr.OnTranslationComplete += new PostProcessor.TranslationCompleteDelegate(C_TranslationComplete); //this event is required for updating the result

//Lets define a thread before running a memory hungry process, because it takes time and usually hangs the UI, so putting in a thread
translatorThread = new Thread(new ThreadStart(pr.TranslateVoid)); //inside the thread is the function that will be executed
translatorThread.Name = "TranslatorThread"; //Name of the thread
translatorThread.Start(); //starting translation.

//TODO: Natural language processing
//The following two lines are something I will work in the future, I was splitting the Document, paragraphs, sentences, and words, for a real natural language processing.
//Will work in this in future when I have more idea and more time.. :)
//Document document = new Document(richTextBoxHindi.Text);

//document.SplitIntoAllParagraphs(richTextBoxHindi.Text);
//this.richTextBoxNepali.Text = document.ReBuildParagraphs();

//End TODO: Natural language processing


//Changing to the respective tab
this.tabControl1.SelectedIndex = 0;
this.tabPageNepali.Focus();

}
/// <summary>
/// Perform translation if everything is okay
/// Preconditions:
/// 1. the rules must be loaded
/// 2. Noun file, pronoun file, verbs file, adjectives file, and the rules file should be present in the application directory
/// 3. Postcondition: the above files in (2) should be editable by the application (I mean they should not be read only)
/// </summary>
public void PerformEnglishTranslation()
{
_IsEditorBusy = true;
string m_translatedStringFromEnglish = "";


// Initialize the translator
NepaliTranslatorHelper t = new NepaliTranslatorHelper();


// Translate the text
try
{
// Forward translation
this.Cursor = Cursors.WaitCursor;
m_translatedStringFromEnglish = t.Parse(richTextBoxHindi.Text);
Thread.Sleep(500); // let Google breathe

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{

this.Cursor = Cursors.Default;
}



//resetting the progressbar to zero
toolStripProgressBar1.ProgressBar.Value = toolStripProgressBar1.ProgressBar.Minimum;

//Loading rules before starting translating
pr = new PostProcessor(SessionsManager.RuleFileName, SessionsManager.VerbListFileName, SessionsManager.NounListFileName, SessionsManager.AdjectiveListFileName, SessionsManager.PronounListFileName, SessionsManager.UsersCustomFileName);

//Getting the source text
//pr.LoadTextToTranslate(richTextBoxHindi.Text);
pr.LoadTextToTranslate(m_translatedStringFromEnglish);


pr.OnFeedback += new PostProcessor.ProgressFeedbackDelgate(C_OnFeedBack); //this event is required for updating the progressbar
pr.OnTranslationComplete += new PostProcessor.TranslationCompleteDelegate(C_TranslationComplete); //this event is required for updating the result

//Lets define a thread before running a memory hungry process, because it takes time and usually hangs the UI, so putting in a thread
translatorThread = new Thread(new ThreadStart(pr.TranslateVoid)); //inside the thread is the function that will be executed
translatorThread.Name = "TranslatorThread"; //Name of the thread
translatorThread.Start(); //starting translation.

//TODO: Natural language processing
//The following two lines are something I will work in the future, I was splitting the Document, paragraphs, sentences, and words, for a real natural language processing.
//Will work in this in future when I have more idea and more time.. :)
//Document document = new Document(richTextBoxHindi.Text);

//document.SplitIntoAllParagraphs(richTextBoxHindi.Text);
//this.richTextBoxNepali.Text = document.ReBuildParagraphs();

//End TODO: Natural language processing


//Changing to the respective tab
this.tabControl1.SelectedIndex = 0;
this.tabPageNepali.Focus();

}







/// <summary>
/// Running the process in a thread so that it would not mess with the GUI
/// </summary>
public void ThreadedTranslation()
{
//TranslatedText = pr.Translate();
PerformTranslationWithGuiUpdates();


}

/// <summary>
/// Dragging file event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DragTextFile(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
foreach (string fileLoc in filePaths)
{
// Code to read the contents of the text file
if (File.Exists(fileLoc))
{
using (TextReader tr = new StreamReader(fileLoc))
{
richTextBoxHindi.Text = "";
//MessageBox.Show(tr.ReadToEnd());
richTextBoxHindi.LoadFile(fileLoc, RichTextBoxStreamType.PlainText);
}
}

}
}


}


/// <summary>
/// Cut function inside Rich text box
/// </summary>
private void CutInsideRTB()
{
if (tabPageHindi.ContainsFocus)
{
if (richTextBoxHindi.SelectionLength > 0)
{
richTextBoxHindi.Cut();
}
}
if (tabPageNepali.ContainsFocus)
{
if (richTextBoxNepali.SelectionLength > 0)
{
richTextBoxNepali.Cut();
}
}
}

/// <summary>
/// Cut function inside Rich text box
/// </summary>
private void SelectAllInsideRTB()
{
if (tabPageHindi.ContainsFocus)
{
richTextBoxHindi.SelectAll();

}
if (tabPageNepali.ContainsFocus)
{
richTextBoxNepali.SelectAll();

}
}


/// <summary>
/// Copy function for rich text box
/// </summary>
private void CopyInsideRTB()
{
if (tabPageHindi.ContainsFocus)
{
if (richTextBoxHindi.SelectionLength > 0)
{
richTextBoxHindi.Copy();
}
}
if (tabPageNepali.ContainsFocus)
{
if (richTextBoxNepali.SelectionLength > 0)
{
richTextBoxNepali.Copy();
}
}
}

/// <summary>
/// Paste menu
/// </summary>
private void PasteInsideRTB()
{
if (tabPageHindi.ContainsFocus)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{

richTextBoxHindi.Paste();

}
}
if (tabPageNepali.ContainsFocus)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
richTextBoxNepali.Paste();
}
}
}

/// <summary>
/// Exits the application
/// </summary>
private void ExitApplication()
{
//Do some household works before exitting
//Save temp data
Application.Exit();
}

/// <summary>
/// Prints the nepali tab
/// </summary>
private void PrintNepaliTab()
{
PrintDialog printDialog1 = new PrintDialog();

PrintDocument DocumentToPrint = new PrintDocument();

printDialog1.Document = DocumentToPrint;

if (printDialog1.ShowDialog() == DialogResult.OK)
{


PrintReader = new StringReader(richTextBoxNepali.Text);
DocumentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
DocumentToPrint.Print();

}



}

/// <summary>
/// Prints page (a generic function for printing)
/// </summary>
private void DocumentToPrint_PrintPage(object sender, PrintPageEventArgs e)
{

#region Code To Print (From http://www.c-sharpcorner.com/winforms/texteditor.asp)

float LinesPerPage = 0;

float YPosition = 0;

int Count = 0;

float LeftMargin = e.MarginBounds.Left;

float TopMargin = e.MarginBounds.Top;



string Line = null;

Font PrintFont = this.richTextBoxNepali.Font;

SolidBrush PrintBrush = new SolidBrush(Color.Black);



LinesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics);



while (Count < LinesPerPage && ((Line = PrintReader.ReadLine()) != null))
{

YPosition = TopMargin + (Count * PrintFont.GetHeight(e.Graphics));

e.Graphics.DrawString(Line, PrintFont, PrintBrush, LeftMargin, YPosition, new StringFormat());

Count++;

}



if (Line != null)
{

e.HasMorePages = true;

}

else
{

e.HasMorePages = false;

}



PrintBrush.Dispose();

#endregion

}

/// <summary>
/// This method informs the progressbar to update, with the appropriate value to update
/// </summary>
/// <param name="percent"></param>
private void C_OnFeedBack (int percent)
{
//this.progressBar1.Value = percent;

//progressBar1.Invoke(new SetPercent(SetPercentMethod), new object[] { percent });
Debug.Write(" "+percent +"%");

//cross thread - so you don't get the cross theading exception

if(toolStripProgressBar1.ProgressBar.InvokeRequired)
{
toolStripProgressBar1.ProgressBar.Invoke(new SetPercent(SetPercentMethod), new object[] {percent});
}
toolStripStatusLabel1.Text = "Translating... ";

}

/// <summary>
/// This method is called when the translation is complete
/// It should have been as simple as : private void C_TranslationComplete(string message){this.richTextBoxNepali.Text = message;}
/// But I kept on receiving error from the compiler and did the lengthy process, hope it will be thread safe now..
/// </summary>
/// <param name="message"></param>
private void C_TranslationComplete(string message)
{
//Microsoft thing Grr..
if(this.richTextBoxNepali.InvokeRequired)
{
//I don't know why but microsoft said: "How to: Make Thread-Safe Calls to Windows Forms Controls" => to use a delegate and use if/else to do this thing..
//looks like some complicated thing .. but the thing we will be doing is inside else..
SetTextCallback d = new SetTextCallback(C_TranslationComplete);
this.Invoke(d, new object[] { message });

//this.richTextBoxNepali.Text = message;

} //End microsoft thing (warning) grrr..
else
{
this.richTextBoxNepali.Text = message; //This is what I actually wanted to do

GetDiff();
//GetDiff(message);
if (this.EnableClipBoardCopyPaste)
{
Clipboard.SetData(TextDataFormat.Text.ToString(), message);
}
toolStripStatusLabel1.Text = "Done ";
this._IsEditorBusy = false;

}




}

/// <summary>
/// Loads wiki article from the internet
/// </summary>
/// <param name="articleName"></param>
public void LoadWikiArticle(string articleName)
{
if(editor!=null)
{
articleContentCache.ArticleName = articleName;
articleContentCache.Article_WikiContent = editor.Open(articleName);
this.richTextBoxHindi.Text = articleContentCache.Article_WikiContent;

}
else
{
if(loggedinUserAWB!=null)
loggedinUserAWB.ShowDialog();
else
{
loggedinUserAWB = new LoginForm(this.SessionsManager);
loggedinUserAWB.ShowDialog();
}
}




}


public void GetDifferences(string originalText,string changedText)
{

try
{
webBrowser.BringToFront();
if (webBrowser.Document == null)
return;

webBrowser.Document.OpenNew(false);
if (changedText == originalText)
{
webBrowser.Document.Write(
@"<h2 style='padding-top: .5em;
padding-bottom: .17em;
border-bottom: 1px solid #aaa;
font-size: 150%;'>No changes</h2><p>Press the ""Source"" button to skip to write in Hindi and click on translate to Nepali to translate.</p>");
}
else
{
// when less than 10 edits show user help info on double click to undo etc.
webBrowser.Document.Write("<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
+ "<html><head>" +
WikiDiff.DiffHead() + @"</head><body>"
+ WikiDiff.TableHeader +
Diff.GetDiff(originalText, changedText, 2) +
"</table>" + /*"<!--<script language='Javascript'>
// Scroll part of the way into the table, disabled due to other interface problems
diffNode=document.getElementById('wikiDiff');
var diffTopY = 0;
while(diffNode) {
diffTopY += diffNode.offsetTop;
diffNode = diffNode.offsetParent;
}
window.scrollTo(0, diffTopY);
</script>-->"*/"</body></html>");
}





}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}

/// <summary>
/// Gets diff from Richtextbox hindi and richtextbox nepali
/// </summary>
private void GetDiff()
{
GetDifferences(richTextBoxHindi.Text,richTextBoxNepali.Text);

}

/// <summary>
/// This function saves the source text in Hindi tab
/// </summary>
private void SaveText()
{
if (richTextBoxHindi != null)
{
if (!string.IsNullOrEmpty(richTextBoxHindi.Text))
{
var textToSave = this.richTextBoxHindi.Text;

if(string.IsNullOrEmpty(ArticleName))
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = ".mediawiki";
if(saveFileDialog.ShowDialog()==DialogResult.OK)
{
ArticleName = saveFileDialog.FileName;
StreamWriter streamWriter = new StreamWriter(ArticleName);
streamWriter.Write(textToSave);
streamWriter.Close();
}

}
else
{
StreamWriter streamWriter = new StreamWriter(ArticleName);
streamWriter.Write(textToSave);
streamWriter.Close();
}


}
}

}


private void C_SetRichTextBoxHindi(string message)
{
if (richTextBoxHindi.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(C_SetRichTextBoxHindi);
this.Invoke(d, new object[] { message });
}
else
{
this.richTextBoxHindi.Text = message;
}

}


private Dictionary<string, string> LoadCorrectionList(string correctionListFileName)
{
Dictionary<string,string> correctionList = new Dictionary<string, string>();
//Create one if it doesnot exist
if (!File.Exists(correctionListFileName))
{
var fileStream = File.Create(correctionListFileName);
fileStream.Close();
}

string tempString;
string inputText = "";

StreamReader sr = new StreamReader(correctionListFileName);
while (!string.IsNullOrEmpty(tempString = sr.ReadLine()))
{
tempString = tempString.Replace(" ", "").Trim();

inputText += (tempString + "\n");
//split the text with |
string[] splittedText = tempString.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
if (splittedText.Length == 2)
{
if (!correctionList.ContainsKey(splittedText[0]))
correctionList.Add(splittedText[0], splittedText[1]);
}

}



sr.Close();
return correctionList;
}




}

}

Change log

r197 by pandey.pandey on Apr 25, 2012   Diff
Adding select all feature
Go to: 
Sign in to write a code review

Older revisions

r181 by pandey.pandey on Mar 24, 2012   Diff
[No log message]
r162 by pandey.pandey on Oct 30, 2011   Diff
Added Bot feature
Added Diff feature while in bot mode
Added delegates for cross thread data
access
Added SessionsManager
...
r161 by pandey.pandey on Oct 29, 2011   Diff
moved translator bot to a separate
class making it independent from
formMainWindow
All revisions of this file

File info

Size: 24086 bytes, 702 lines
Powered by Google Project Hosting