From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / OSX / textclient-iOS.m
1 /* xscreensaver, Copyright (c) 2012-2016 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  *
11  * Loading URLs and returning the underlying text.
12  *
13  * This is necessary because iOS doesn't have Perl installed, so we can't
14  * run "xscreensaver-text" to do this.
15  */
16
17 #include "utils.h"
18
19 #ifdef USE_IPHONE // whole file
20
21 #include "textclient.h"
22
23 char *
24 textclient_mobile_date_string (void)
25 {
26   UIDevice *dd = [UIDevice currentDevice];
27   NSString *name = [dd name];                   // My iPhone
28   NSString *model = [dd model];                 // iPad
29   // NSString *system = [dd systemName];        // iPhone OS
30   NSString *vers = [dd systemVersion];          // 5.0
31   NSString *date =
32     [NSDateFormatter
33       localizedStringFromDate:[NSDate date]
34       dateStyle: NSDateFormatterMediumStyle
35       timeStyle: NSDateFormatterMediumStyle];
36   NSString *nl = @"\n";
37
38   NSString *result = name;
39   result = [result stringByAppendingString: nl];
40   result = [result stringByAppendingString: model];
41   // result = [result stringByAppendingString: nl];
42   // result = [result stringByAppendingString: system];
43   result = [result stringByAppendingString: @" "];
44   result = [result stringByAppendingString: vers];
45   result = [result stringByAppendingString: nl];
46   result = [result stringByAppendingString: nl];
47   result = [result stringByAppendingString: date];
48   result = [result stringByAppendingString: nl];
49   result = [result stringByAppendingString: nl];
50   return strdup ([result cStringUsingEncoding:NSISOLatin1StringEncoding]);
51 }
52
53
54 /* Returns the contents of the URL. */
55 char *
56 textclient_mobile_url_string (Display *dpy, const char *url)
57 {
58   NSURL *nsurl =
59     [NSURL URLWithString:
60              [NSString stringWithCString: url
61                        encoding:NSISOLatin1StringEncoding]];
62   NSString *body =
63     [NSString stringWithContentsOfURL: nsurl
64               encoding: NSUTF8StringEncoding
65               error: nil];
66   return (body
67           ? strdup ([body cStringUsingEncoding:NSUTF8StringEncoding])
68           : 0);
69 }
70
71 #endif // USE_IPHONE -- whole file