Skip to content

Commit

Permalink
gtk3, refactor for better resource management and readability
Browse files Browse the repository at this point in the history
Refactored various methods in MainWindow.cs, ReportViewer.cs, and Program.cs to use the `using var` statement for disposable objects, ensuring proper disposal and preventing resource leaks. Improved readability and error handling in several methods, enhancing code maintainability and robustness.
  • Loading branch information
majorsilence committed Feb 8, 2025
1 parent 68bdf29 commit ddb464d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 64 deletions.
52 changes: 24 additions & 28 deletions RdlGtk3/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,41 +169,37 @@ protected async void OnFileOpen_Activated(object sender, System.EventArgs e)
param[2] = "Open";
param[3] = Gtk.ResponseType.Accept;

using (Gtk.FileChooserDialog fc =
using var fc =
new Gtk.FileChooserDialog("Open File",
null,
Gtk.FileChooserAction.Open,
param))
param);

if (fc.Run() != (int)Gtk.ResponseType.Accept)
{
fc.Destroy();
return;
}

if (fc.Run() != (int)Gtk.ResponseType.Accept)
{
fc.Destroy();
return;
}
try
{
string filename = fc.Filename;
fc.Destroy();

try
if (System.IO.File.Exists(filename))
{
string filename = fc.Filename;
fc.Destroy();

if (System.IO.File.Exists(filename))
{
string parameters = await this.GetParameters(new Uri(filename));
await this.reportviewer1.LoadReport(new Uri(filename), parameters);
}
}
catch (Exception ex)
{
using (Gtk.MessageDialog m = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Info,
Gtk.ButtonsType.Ok, false,
"Error Opening File." + System.Environment.NewLine + ex.Message))
{
m.Run();
m.Destroy();
}
string parameters = await this.GetParameters(new Uri(filename));
await this.reportviewer1.LoadReport(new Uri(filename), parameters);
}
}
catch (Exception ex)
{
using var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Info,
Gtk.ButtonsType.Ok, false,
"Error Opening File." + System.Environment.NewLine + ex.Message);
m.Run();
m.Destroy();
}

}

Expand Down Expand Up @@ -241,7 +237,7 @@ private async Task<string> GetParameters(Uri sourcefile)
{
string parameters = "";
string sourceRdl = System.IO.File.ReadAllText(sourcefile.LocalPath);
Majorsilence.Reporting.Rdl.RDLParser parser = new Majorsilence.Reporting.Rdl.RDLParser(sourceRdl);
var parser = new Rdl.RDLParser(sourceRdl);
await parser.Parse();

if (parser.Report.UserReportParameters.Count > 0)
Expand All @@ -251,7 +247,7 @@ private async Task<string> GetParameters(Uri sourcefile)
parameters += "&" + rp.Name + "=";
}

Majorsilence.Reporting.RdlGtk3.ParameterPrompt prompt = new Majorsilence.Reporting.RdlGtk3.ParameterPrompt();
using var prompt = new ParameterPrompt();
prompt.Parameters = parameters;

if (prompt.Run() == (int)Gtk.ResponseType.Ok)
Expand Down
59 changes: 24 additions & 35 deletions RdlGtk3/ReportViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public async Task SaveReport()
param[2] = Strings.ButtonSave_Text;
param[3] = Gtk.ResponseType.Accept;

Gtk.FileChooserDialog fc =
using var fc =
new Gtk.FileChooserDialog(Strings.FileChooser_SaveFileTo_Title,
null,
Gtk.FileChooserAction.Save,
Expand Down Expand Up @@ -643,7 +643,7 @@ public async Task SaveReport()

if (!fc.Filters.Any())
{
Gtk.MessageDialog m = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Info,
using var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Info,
Gtk.ButtonsType.Ok, false,
"Export in all document formats is prohibited");

Expand Down Expand Up @@ -755,7 +755,7 @@ public async Task SaveReport()
if (files[i] == filename)
{
//If found files with the same name in directory
MessageDialog m = new Gtk.MessageDialog(null, DialogFlags.Modal, MessageType.Question,
using var m = new Gtk.MessageDialog(null, DialogFlags.Modal, MessageType.Question,
Gtk.ButtonsType.YesNo, false,
Strings.SaveToFile_CheckIf_SameFilesInDir);

Expand Down Expand Up @@ -795,7 +795,7 @@ public async Task SaveReport()
}
catch (Exception ex)
{
Gtk.MessageDialog m = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Info,
using var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Info,
Gtk.ButtonsType.Ok, false,
$"Error Saving Copy of {fc.Filter?.Name}." + System.Environment.NewLine + ex.Message);

Expand All @@ -819,29 +819,20 @@ public async Task SaveReport()
/// </param>
private async Task ExportReport(Report report, string FileName, OutputPresentationType exportType)
{
OneFileStreamGen sg = null;

try
{
sg = new OneFileStreamGen(FileName, true);
using var sg = new OneFileStreamGen(FileName, true);
await report.RunRender(sg, exportType);
}
catch (Exception ex)
{
Gtk.MessageDialog m = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Error,
using var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Error,
Gtk.ButtonsType.Ok, false,
ex.Message);

m.Run();
m.Destroy();
}
finally
{
if (sg != null)
{
sg.CloseMainStream();
}
}
return;
}

Expand All @@ -851,27 +842,25 @@ protected void OnPrintActionActivated(object sender, System.EventArgs e)

public void PrintReport()
{
using (PrintContext context = new PrintContext(Window.Handle))
using PrintContext context = new PrintContext(Window.Handle);
if (customPrintAction == null)
{
if (customPrintAction == null)
{
printing = new PrintOperation();
printing.Unit = Unit.Points;
printing.UseFullPage = true;
printing.DefaultPageSetup = new PageSetup();
printing.DefaultPageSetup.Orientation =
report.PageHeightPoints > report.PageWidthPoints ? PageOrientation.Portrait : PageOrientation.Landscape;

printing.BeginPrint += HandlePrintBeginPrint;
printing.DrawPage += HandlePrintDrawPage;
printing.EndPrint += HandlePrintEndPrint;

printing.Run(PrintOperationAction.PrintDialog, null);
}
else
{
customPrintAction.Invoke(pages);
}
printing = new PrintOperation();
printing.Unit = Unit.Points;
printing.UseFullPage = true;
printing.DefaultPageSetup = new PageSetup();
printing.DefaultPageSetup.Orientation =
report.PageHeightPoints > report.PageWidthPoints ? PageOrientation.Portrait : PageOrientation.Landscape;

printing.BeginPrint += HandlePrintBeginPrint;
printing.DrawPage += HandlePrintDrawPage;
printing.EndPrint += HandlePrintEndPrint;

printing.Run(PrintOperationAction.PrintDialog, null);
}
else
{
customPrintAction.Invoke(pages);
}
}

Expand Down
2 changes: 1 addition & 1 deletion RdlGtk3Viewer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MainClass
public static void Main(string[] args)
{
Application.Init();
var win = new Majorsilence.Reporting.RdlGtk3.MainWindow();
using var win = new Majorsilence.Reporting.RdlGtk3.MainWindow();
win.Show();
Application.Run();
}
Expand Down

0 comments on commit ddb464d

Please sign in to comment.