project_files/HedgewarsMobile/Classes/GameLogViewController.m
branchqmlfrontend
changeset 11403 b894922d58cc
parent 11177 015596575698
child 12872 00215a7ec5f5
equal deleted inserted replaced
11076:fcbdee9cdd74 11403:b894922d58cc
       
     1 /*
       
     2  * Hedgewars-iOS, a Hedgewars port for iOS devices
       
     3  * Copyright (c) 2015 Anton Malmygin <antonc27@mail.ru>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
       
    17  */
       
    18 
       
    19 #import "GameLogViewController.h"
       
    20 
       
    21 #ifdef DEBUG
       
    22 #import <MessageUI/MFMailComposeViewController.h>
       
    23 #endif
       
    24 
       
    25 @interface GameLogViewController ()
       
    26 #ifdef DEBUG
       
    27 <MFMailComposeViewControllerDelegate>
       
    28 #endif
       
    29 
       
    30 @end
       
    31 
       
    32 @implementation GameLogViewController
       
    33 
       
    34 #pragma mark - View life cycle
       
    35 
       
    36 - (void)viewDidLoad
       
    37 {
       
    38     [super viewDidLoad];
       
    39     
       
    40     self.title = @"Last game log";
       
    41     
       
    42     UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissAction)];
       
    43     self.navigationItem.rightBarButtonItem = closeButton;
       
    44     [closeButton release];
       
    45     
       
    46 #ifdef DEBUG
       
    47     if ([self allowSendLogByEmail])
       
    48     {
       
    49         UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:@"Send" style:UIBarButtonItemStylePlain target:self action:@selector(sendLogByEmailAction)];
       
    50         self.navigationItem.leftBarButtonItem = sendButton;
       
    51         [sendButton release];
       
    52     }
       
    53 #endif
       
    54     
       
    55     NSString *debugStr = nil;
       
    56     if ([[NSFileManager defaultManager] fileExistsAtPath:DEBUG_FILE()])
       
    57         debugStr = [[NSString alloc] initWithContentsOfFile:DEBUG_FILE() encoding:NSUTF8StringEncoding error:nil];
       
    58     else
       
    59         debugStr = [[NSString alloc] initWithString:@"Here be log"];
       
    60     
       
    61     UITextView *logView = [[UITextView alloc] initWithFrame:self.view.frame];
       
    62     [logView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
       
    63     logView.text = debugStr;
       
    64     [debugStr release];
       
    65     logView.editable = NO;
       
    66     
       
    67     [self.view addSubview:logView];
       
    68     [logView release];
       
    69 }
       
    70 
       
    71 #pragma mark - Parameters
       
    72 
       
    73 #ifdef DEBUG
       
    74 - (BOOL)allowSendLogByEmail
       
    75 {
       
    76     return ([MFMailComposeViewController canSendMail] && [[NSFileManager defaultManager] fileExistsAtPath:DEBUG_FILE()]);
       
    77 }
       
    78 #endif
       
    79 
       
    80 #pragma mark - Actions
       
    81 
       
    82 #ifdef DEBUG
       
    83 - (void)sendLogByEmailAction
       
    84 {
       
    85     MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
       
    86     picker.mailComposeDelegate = self;
       
    87     [picker setSubject:@"Log file of iHedgewars game"];
       
    88     
       
    89     // Attach a log file to the email
       
    90     NSData *logData = [NSData dataWithContentsOfFile:DEBUG_FILE()];
       
    91     [picker addAttachmentData:logData mimeType:@"text/plain" fileName:@"game0.log"];
       
    92     
       
    93     // Fill out the email body text
       
    94     NSString *emailBody = @"Add here description of a problem/log";
       
    95     [picker setMessageBody:emailBody isHTML:NO];
       
    96     
       
    97     [self presentViewController:picker animated:YES completion:nil];
       
    98     [picker release];
       
    99 }
       
   100 #endif
       
   101 
       
   102 - (void)dismissAction
       
   103 {
       
   104     [self dismissViewControllerAnimated:YES completion:nil];
       
   105 }
       
   106 
       
   107 #pragma mark - MailCompose delegate
       
   108 
       
   109 #ifdef DEBUG
       
   110 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
       
   111 {
       
   112     // Notifies users about errors associated with the interface
       
   113     switch (result)
       
   114     {
       
   115         case MFMailComposeResultCancelled:
       
   116             NSLog(@"MailComposeResult: canceled");
       
   117             break;
       
   118         case MFMailComposeResultSaved:
       
   119             NSLog(@"MailComposeResult: saved");
       
   120             break;
       
   121         case MFMailComposeResultSent:
       
   122             NSLog(@"MailComposeResult: sent");
       
   123             break;
       
   124         case MFMailComposeResultFailed:
       
   125             NSLog(@"MailComposeResult: failed");
       
   126             break;
       
   127         default:
       
   128             NSLog(@"MailComposeResult: not sent");
       
   129             break;
       
   130     }
       
   131     
       
   132     [self dismissViewControllerAnimated:YES completion:nil];
       
   133 }
       
   134 #endif
       
   135 
       
   136 #pragma mark - Memory warning
       
   137 
       
   138 - (void)didReceiveMemoryWarning
       
   139 {
       
   140     [super didReceiveMemoryWarning];
       
   141     // Dispose of any resources that can be recreated.
       
   142 }
       
   143 
       
   144 @end